Command substitution not working inside for loop condition

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done



Gives me an error:



-bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")










share|improve this question


























    0















    for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done



    Gives me an error:



    -bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")










    share|improve this question
























      0












      0








      0








      for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done



      Gives me an error:



      -bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")










      share|improve this question














      for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done



      Gives me an error:



      -bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")







      bash for loop-device






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 22 at 17:03









      user323587user323587

      132




      132




















          2 Answers
          2






          active

          oldest

          votes


















          4














          The output of wc -l sedtest1 will be something like:



          21 sedtest1


          So the test will become something like a <= 1234 sedtest1 which is invalid.



          Also, that does mean that the wc command will be run for each iteration of the loop. If the content of the sedtest1 file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:



          n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
          for ((a = 0; a < n; a++)); do echo "$a"; done


          I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.






          share|improve this answer























          • Thanks for the sources, I'll look into them!

            – user323587
            Jan 22 at 17:30


















          0














          Nevermind I was just being incredibly stupid...
          wc -l file outputs as "x file".



          All I had to do was this:
          for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done






          share|improve this answer


















          • 1





            Umm... Now you've made the code worse. The wc and awk will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.

            – Kusalananda
            Jan 22 at 17:16











          • Oh I didn't say my answer was better, I just figured out the problem :D

            – user323587
            Jan 22 at 17:28










          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',
          autoActivateHeartbeat: false,
          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%2f496011%2fcommand-substitution-not-working-inside-for-loop-condition%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          The output of wc -l sedtest1 will be something like:



          21 sedtest1


          So the test will become something like a <= 1234 sedtest1 which is invalid.



          Also, that does mean that the wc command will be run for each iteration of the loop. If the content of the sedtest1 file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:



          n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
          for ((a = 0; a < n; a++)); do echo "$a"; done


          I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.






          share|improve this answer























          • Thanks for the sources, I'll look into them!

            – user323587
            Jan 22 at 17:30















          4














          The output of wc -l sedtest1 will be something like:



          21 sedtest1


          So the test will become something like a <= 1234 sedtest1 which is invalid.



          Also, that does mean that the wc command will be run for each iteration of the loop. If the content of the sedtest1 file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:



          n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
          for ((a = 0; a < n; a++)); do echo "$a"; done


          I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.






          share|improve this answer























          • Thanks for the sources, I'll look into them!

            – user323587
            Jan 22 at 17:30













          4












          4








          4







          The output of wc -l sedtest1 will be something like:



          21 sedtest1


          So the test will become something like a <= 1234 sedtest1 which is invalid.



          Also, that does mean that the wc command will be run for each iteration of the loop. If the content of the sedtest1 file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:



          n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
          for ((a = 0; a < n; a++)); do echo "$a"; done


          I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.






          share|improve this answer













          The output of wc -l sedtest1 will be something like:



          21 sedtest1


          So the test will become something like a <= 1234 sedtest1 which is invalid.



          Also, that does mean that the wc command will be run for each iteration of the loop. If the content of the sedtest1 file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:



          n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename
          for ((a = 0; a < n; a++)); do echo "$a"; done


          I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 at 17:09









          Stéphane ChazelasStéphane Chazelas

          305k57574928




          305k57574928












          • Thanks for the sources, I'll look into them!

            – user323587
            Jan 22 at 17:30

















          • Thanks for the sources, I'll look into them!

            – user323587
            Jan 22 at 17:30
















          Thanks for the sources, I'll look into them!

          – user323587
          Jan 22 at 17:30





          Thanks for the sources, I'll look into them!

          – user323587
          Jan 22 at 17:30













          0














          Nevermind I was just being incredibly stupid...
          wc -l file outputs as "x file".



          All I had to do was this:
          for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done






          share|improve this answer


















          • 1





            Umm... Now you've made the code worse. The wc and awk will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.

            – Kusalananda
            Jan 22 at 17:16











          • Oh I didn't say my answer was better, I just figured out the problem :D

            – user323587
            Jan 22 at 17:28















          0














          Nevermind I was just being incredibly stupid...
          wc -l file outputs as "x file".



          All I had to do was this:
          for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done






          share|improve this answer


















          • 1





            Umm... Now you've made the code worse. The wc and awk will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.

            – Kusalananda
            Jan 22 at 17:16











          • Oh I didn't say my answer was better, I just figured out the problem :D

            – user323587
            Jan 22 at 17:28













          0












          0








          0







          Nevermind I was just being incredibly stupid...
          wc -l file outputs as "x file".



          All I had to do was this:
          for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done






          share|improve this answer













          Nevermind I was just being incredibly stupid...
          wc -l file outputs as "x file".



          All I had to do was this:
          for (( a=1;a<=$(wc -l sedtest1|awk 'print $1');a++ ));do echo $a; done







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 at 17:09









          user323587user323587

          132




          132







          • 1





            Umm... Now you've made the code worse. The wc and awk will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.

            – Kusalananda
            Jan 22 at 17:16











          • Oh I didn't say my answer was better, I just figured out the problem :D

            – user323587
            Jan 22 at 17:28












          • 1





            Umm... Now you've made the code worse. The wc and awk will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.

            – Kusalananda
            Jan 22 at 17:16











          • Oh I didn't say my answer was better, I just figured out the problem :D

            – user323587
            Jan 22 at 17:28







          1




          1





          Umm... Now you've made the code worse. The wc and awk will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.

          – Kusalananda
          Jan 22 at 17:16





          Umm... Now you've made the code worse. The wc and awk will be invoked in each iteration of the loop. If the file is large, this would run very slowly. See Stéphane Chazelas' answer.

          – Kusalananda
          Jan 22 at 17:16













          Oh I didn't say my answer was better, I just figured out the problem :D

          – user323587
          Jan 22 at 17:28





          Oh I didn't say my answer was better, I just figured out the problem :D

          – user323587
          Jan 22 at 17:28

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496011%2fcommand-substitution-not-working-inside-for-loop-condition%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

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay