Looping through an array with for gives different results











up vote
0
down vote

favorite












I'm having a somewhat of a problem with an array in bash.



Given this array:



 krn=(linux-image-4.15.0-30-generic linux-image-4.15.0-32-generic linux-image-4.15.0-33-generic)


If I use the following for loop to iterate through it, I get different results without changing anything:



1st time:



for krn in ${krn[@]};do echo $krn;done
linux-image-4.15.0-30-generic
linux-image-4.15.0-32-generic
linux-image-4.15.0-33-generic


2nd time:



for krn in ${krn[@]};do echo $krn;done
linux-image-4.15.0-33-generic
linux-image-4.15.0-32-generic
linux-image-4.15.0-33-generic


What am I doing wrong?



If I use a C style for with array lenght, it doesn't have this strange behaviour.



for ((i=0; i<${#krn[@]}; i++)); do echo ${krn[i]};done









share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm having a somewhat of a problem with an array in bash.



    Given this array:



     krn=(linux-image-4.15.0-30-generic linux-image-4.15.0-32-generic linux-image-4.15.0-33-generic)


    If I use the following for loop to iterate through it, I get different results without changing anything:



    1st time:



    for krn in ${krn[@]};do echo $krn;done
    linux-image-4.15.0-30-generic
    linux-image-4.15.0-32-generic
    linux-image-4.15.0-33-generic


    2nd time:



    for krn in ${krn[@]};do echo $krn;done
    linux-image-4.15.0-33-generic
    linux-image-4.15.0-32-generic
    linux-image-4.15.0-33-generic


    What am I doing wrong?



    If I use a C style for with array lenght, it doesn't have this strange behaviour.



    for ((i=0; i<${#krn[@]}; i++)); do echo ${krn[i]};done









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm having a somewhat of a problem with an array in bash.



      Given this array:



       krn=(linux-image-4.15.0-30-generic linux-image-4.15.0-32-generic linux-image-4.15.0-33-generic)


      If I use the following for loop to iterate through it, I get different results without changing anything:



      1st time:



      for krn in ${krn[@]};do echo $krn;done
      linux-image-4.15.0-30-generic
      linux-image-4.15.0-32-generic
      linux-image-4.15.0-33-generic


      2nd time:



      for krn in ${krn[@]};do echo $krn;done
      linux-image-4.15.0-33-generic
      linux-image-4.15.0-32-generic
      linux-image-4.15.0-33-generic


      What am I doing wrong?



      If I use a C style for with array lenght, it doesn't have this strange behaviour.



      for ((i=0; i<${#krn[@]}; i++)); do echo ${krn[i]};done









      share|improve this question













      I'm having a somewhat of a problem with an array in bash.



      Given this array:



       krn=(linux-image-4.15.0-30-generic linux-image-4.15.0-32-generic linux-image-4.15.0-33-generic)


      If I use the following for loop to iterate through it, I get different results without changing anything:



      1st time:



      for krn in ${krn[@]};do echo $krn;done
      linux-image-4.15.0-30-generic
      linux-image-4.15.0-32-generic
      linux-image-4.15.0-33-generic


      2nd time:



      for krn in ${krn[@]};do echo $krn;done
      linux-image-4.15.0-33-generic
      linux-image-4.15.0-32-generic
      linux-image-4.15.0-33-generic


      What am I doing wrong?



      If I use a C style for with array lenght, it doesn't have this strange behaviour.



      for ((i=0; i<${#krn[@]}; i++)); do echo ${krn[i]};done






      bash shell scripting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Costin Balan

      437




      437






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Your loop uses krn as the loop variable. This is also the name of the array that you loop over. The shell does not maintain separate name spaces for array variables and non-array variables.



          For each iteration, krn will be set to the current value from the array. This has the effect of modifying the array's first element in every iteration. The loop modifies the array so that the first element will be a copy of the last element after the end of the loop.



          To correct this, choose another name for the loop variable, or for the array.





          Additionally, you should use "${krn[@]}" (the expansion, double quoted) in the loop head as using ${krn[@]} unquoted would split each element on whitespaces (or whatever is in $IFS) and invoke filename globbing on the resulting words. The same goes for echo "${krn[i]}" in the later loop.



          Assuming there is at least one element in the array, the whole loop could alternatively be replaced by



          printf '%sn' "${krn[@]}"





          share|improve this answer























          • Thanks, this solved my issue. Also, I will use the alternative because it's better and smarter.
            – Costin Balan
            yesterday











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482101%2flooping-through-an-array-with-for-gives-different-results%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Your loop uses krn as the loop variable. This is also the name of the array that you loop over. The shell does not maintain separate name spaces for array variables and non-array variables.



          For each iteration, krn will be set to the current value from the array. This has the effect of modifying the array's first element in every iteration. The loop modifies the array so that the first element will be a copy of the last element after the end of the loop.



          To correct this, choose another name for the loop variable, or for the array.





          Additionally, you should use "${krn[@]}" (the expansion, double quoted) in the loop head as using ${krn[@]} unquoted would split each element on whitespaces (or whatever is in $IFS) and invoke filename globbing on the resulting words. The same goes for echo "${krn[i]}" in the later loop.



          Assuming there is at least one element in the array, the whole loop could alternatively be replaced by



          printf '%sn' "${krn[@]}"





          share|improve this answer























          • Thanks, this solved my issue. Also, I will use the alternative because it's better and smarter.
            – Costin Balan
            yesterday















          up vote
          2
          down vote



          accepted










          Your loop uses krn as the loop variable. This is also the name of the array that you loop over. The shell does not maintain separate name spaces for array variables and non-array variables.



          For each iteration, krn will be set to the current value from the array. This has the effect of modifying the array's first element in every iteration. The loop modifies the array so that the first element will be a copy of the last element after the end of the loop.



          To correct this, choose another name for the loop variable, or for the array.





          Additionally, you should use "${krn[@]}" (the expansion, double quoted) in the loop head as using ${krn[@]} unquoted would split each element on whitespaces (or whatever is in $IFS) and invoke filename globbing on the resulting words. The same goes for echo "${krn[i]}" in the later loop.



          Assuming there is at least one element in the array, the whole loop could alternatively be replaced by



          printf '%sn' "${krn[@]}"





          share|improve this answer























          • Thanks, this solved my issue. Also, I will use the alternative because it's better and smarter.
            – Costin Balan
            yesterday













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Your loop uses krn as the loop variable. This is also the name of the array that you loop over. The shell does not maintain separate name spaces for array variables and non-array variables.



          For each iteration, krn will be set to the current value from the array. This has the effect of modifying the array's first element in every iteration. The loop modifies the array so that the first element will be a copy of the last element after the end of the loop.



          To correct this, choose another name for the loop variable, or for the array.





          Additionally, you should use "${krn[@]}" (the expansion, double quoted) in the loop head as using ${krn[@]} unquoted would split each element on whitespaces (or whatever is in $IFS) and invoke filename globbing on the resulting words. The same goes for echo "${krn[i]}" in the later loop.



          Assuming there is at least one element in the array, the whole loop could alternatively be replaced by



          printf '%sn' "${krn[@]}"





          share|improve this answer














          Your loop uses krn as the loop variable. This is also the name of the array that you loop over. The shell does not maintain separate name spaces for array variables and non-array variables.



          For each iteration, krn will be set to the current value from the array. This has the effect of modifying the array's first element in every iteration. The loop modifies the array so that the first element will be a copy of the last element after the end of the loop.



          To correct this, choose another name for the loop variable, or for the array.





          Additionally, you should use "${krn[@]}" (the expansion, double quoted) in the loop head as using ${krn[@]} unquoted would split each element on whitespaces (or whatever is in $IFS) and invoke filename globbing on the resulting words. The same goes for echo "${krn[i]}" in the later loop.



          Assuming there is at least one element in the array, the whole loop could alternatively be replaced by



          printf '%sn' "${krn[@]}"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Kusalananda

          115k15218351




          115k15218351












          • Thanks, this solved my issue. Also, I will use the alternative because it's better and smarter.
            – Costin Balan
            yesterday


















          • Thanks, this solved my issue. Also, I will use the alternative because it's better and smarter.
            – Costin Balan
            yesterday
















          Thanks, this solved my issue. Also, I will use the alternative because it's better and smarter.
          – Costin Balan
          yesterday




          Thanks, this solved my issue. Also, I will use the alternative because it's better and smarter.
          – Costin Balan
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482101%2flooping-through-an-array-with-for-gives-different-results%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Accessing regular linux commands in Huawei's Dopra Linux

          Can't connect RFCOMM socket: Host is down

          Kernel panic - not syncing: Fatal Exception in Interrupt