While loop in Bash script?












7















I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:



#!/bin/bash
while read myline
do
done

echo "Hello"
while read line
do
done

exit 0


The output I get is:



./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'


and my bash version is:



GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)


Thank you.





Edit: The script works OK when the while loop isn't empty.



While I'm at it... I expected to exit the loop when the user typed nothing, ie. simply hit the Enter key, but Bash keeps looping. How can I exit the loop?



while read myline
do
echo ${myline}
done

echo "Hello"
while read line
do
true
done

exit 0









share|improve this question




















  • 1





    This simple read-loop works until "end-of-file". You can give an end-of-file by typing control-D.

    – Paŭlo Ebermann
    Feb 25 '11 at 16:33
















7















I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:



#!/bin/bash
while read myline
do
done

echo "Hello"
while read line
do
done

exit 0


The output I get is:



./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'


and my bash version is:



GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)


Thank you.





Edit: The script works OK when the while loop isn't empty.



While I'm at it... I expected to exit the loop when the user typed nothing, ie. simply hit the Enter key, but Bash keeps looping. How can I exit the loop?



while read myline
do
echo ${myline}
done

echo "Hello"
while read line
do
true
done

exit 0









share|improve this question




















  • 1





    This simple read-loop works until "end-of-file". You can give an end-of-file by typing control-D.

    – Paŭlo Ebermann
    Feb 25 '11 at 16:33














7












7








7








I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:



#!/bin/bash
while read myline
do
done

echo "Hello"
while read line
do
done

exit 0


The output I get is:



./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'


and my bash version is:



GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)


Thank you.





Edit: The script works OK when the while loop isn't empty.



While I'm at it... I expected to exit the loop when the user typed nothing, ie. simply hit the Enter key, but Bash keeps looping. How can I exit the loop?



while read myline
do
echo ${myline}
done

echo "Hello"
while read line
do
true
done

exit 0









share|improve this question
















I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:



#!/bin/bash
while read myline
do
done

echo "Hello"
while read line
do
done

exit 0


The output I get is:



./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'


and my bash version is:



GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)


Thank you.





Edit: The script works OK when the while loop isn't empty.



While I'm at it... I expected to exit the loop when the user typed nothing, ie. simply hit the Enter key, but Bash keeps looping. How can I exit the loop?



while read myline
do
echo ${myline}
done

echo "Hello"
while read line
do
true
done

exit 0






linux bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 3 '15 at 8:27









paxdiablo

631k16912431666




631k16912431666










asked Feb 24 '11 at 12:07









GulbaharGulbahar

2,236175380




2,236175380








  • 1





    This simple read-loop works until "end-of-file". You can give an end-of-file by typing control-D.

    – Paŭlo Ebermann
    Feb 25 '11 at 16:33














  • 1





    This simple read-loop works until "end-of-file". You can give an end-of-file by typing control-D.

    – Paŭlo Ebermann
    Feb 25 '11 at 16:33








1




1





This simple read-loop works until "end-of-file". You can give an end-of-file by typing control-D.

– Paŭlo Ebermann
Feb 25 '11 at 16:33





This simple read-loop works until "end-of-file". You can give an end-of-file by typing control-D.

– Paŭlo Ebermann
Feb 25 '11 at 16:33












6 Answers
6






active

oldest

votes


















14














You can't have an empty loop. If you want a placeholder, just use true.



#!/bin/bash
while read myline
do
true
done


or, more likely, do something useful with the input:



#!/bin/bash
while read myline
do
echo "You entered [$line]"
done




As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:



#!/bin/bash
read line
while [[ "$line" != "" ]] ; do
echo "You entered [$line]"
read line
done





share|improve this answer


























  • Thanks, that solved the issue.

    – Gulbahar
    Feb 24 '11 at 12:23











  • Thanks again. Next time, I'll post a new question instead.

    – Gulbahar
    Feb 24 '11 at 12:35











  • while :; do :; done work (: is a synonym for true)

    – Fixee
    Aug 29 '14 at 1:42



















0














When you are using the read command in a while loop it need input:



echo "Hello" | while read line ; do echo $line ; done


or using several lines:



echo "Hello" | while read line
do
echo $line
done





share|improve this answer































    0














    What are you trying to do? From the look of it it seems that you are trying to read into a variable?



    This is done by simply stating read the value can then be found inside of $



    ex:



    read myvar

    echo $myvar


    As other have stated the trouble with the loop is that it is empty which is not allowed.






    share|improve this answer































      0














      This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.



      while
      read -r line
      [[ $line ]]
      do
      :
      done


      When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.



      while
      save=$line
      read -r line
      [[ $line ]]
      do
      :
      done





      share|improve this answer


























      • Dennis, did you mean to init line with something other than ''? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well.

        – paxdiablo
        Jul 22 '13 at 22:53













      • @paxdiablo: You are correct. Other languages have a proper do loop with a test at the end, but Bash does not. My answer here shows how to emulate a do while loop in Bash. I've updated this answer to match that one.

        – Dennis Williamson
        Jul 23 '13 at 0:14



















      0














      If you are looking to create a menu with choices as a infinite loop (with the choice to break out)



      PS3='Please enter your choice: '
      options=("hello" "date" "quit")
      select opt in "${options[@]}"
      do
      case $opt in
      "hello") echo "world";;
      "date") echo $(date);;
      "quit")
      break;;
      *) echo "invalid option";;
      esac
      done


      PS3 is described here






      share|improve this answer































        -1














        If you type help while in bash you'll get an explanation of the command.






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f5104426%2fwhile-loop-in-bash-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          14














          You can't have an empty loop. If you want a placeholder, just use true.



          #!/bin/bash
          while read myline
          do
          true
          done


          or, more likely, do something useful with the input:



          #!/bin/bash
          while read myline
          do
          echo "You entered [$line]"
          done




          As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:



          #!/bin/bash
          read line
          while [[ "$line" != "" ]] ; do
          echo "You entered [$line]"
          read line
          done





          share|improve this answer


























          • Thanks, that solved the issue.

            – Gulbahar
            Feb 24 '11 at 12:23











          • Thanks again. Next time, I'll post a new question instead.

            – Gulbahar
            Feb 24 '11 at 12:35











          • while :; do :; done work (: is a synonym for true)

            – Fixee
            Aug 29 '14 at 1:42
















          14














          You can't have an empty loop. If you want a placeholder, just use true.



          #!/bin/bash
          while read myline
          do
          true
          done


          or, more likely, do something useful with the input:



          #!/bin/bash
          while read myline
          do
          echo "You entered [$line]"
          done




          As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:



          #!/bin/bash
          read line
          while [[ "$line" != "" ]] ; do
          echo "You entered [$line]"
          read line
          done





          share|improve this answer


























          • Thanks, that solved the issue.

            – Gulbahar
            Feb 24 '11 at 12:23











          • Thanks again. Next time, I'll post a new question instead.

            – Gulbahar
            Feb 24 '11 at 12:35











          • while :; do :; done work (: is a synonym for true)

            – Fixee
            Aug 29 '14 at 1:42














          14












          14








          14







          You can't have an empty loop. If you want a placeholder, just use true.



          #!/bin/bash
          while read myline
          do
          true
          done


          or, more likely, do something useful with the input:



          #!/bin/bash
          while read myline
          do
          echo "You entered [$line]"
          done




          As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:



          #!/bin/bash
          read line
          while [[ "$line" != "" ]] ; do
          echo "You entered [$line]"
          read line
          done





          share|improve this answer















          You can't have an empty loop. If you want a placeholder, just use true.



          #!/bin/bash
          while read myline
          do
          true
          done


          or, more likely, do something useful with the input:



          #!/bin/bash
          while read myline
          do
          echo "You entered [$line]"
          done




          As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:



          #!/bin/bash
          read line
          while [[ "$line" != "" ]] ; do
          echo "You entered [$line]"
          read line
          done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 3 '15 at 8:23

























          answered Feb 24 '11 at 12:10









          paxdiablopaxdiablo

          631k16912431666




          631k16912431666













          • Thanks, that solved the issue.

            – Gulbahar
            Feb 24 '11 at 12:23











          • Thanks again. Next time, I'll post a new question instead.

            – Gulbahar
            Feb 24 '11 at 12:35











          • while :; do :; done work (: is a synonym for true)

            – Fixee
            Aug 29 '14 at 1:42



















          • Thanks, that solved the issue.

            – Gulbahar
            Feb 24 '11 at 12:23











          • Thanks again. Next time, I'll post a new question instead.

            – Gulbahar
            Feb 24 '11 at 12:35











          • while :; do :; done work (: is a synonym for true)

            – Fixee
            Aug 29 '14 at 1:42

















          Thanks, that solved the issue.

          – Gulbahar
          Feb 24 '11 at 12:23





          Thanks, that solved the issue.

          – Gulbahar
          Feb 24 '11 at 12:23













          Thanks again. Next time, I'll post a new question instead.

          – Gulbahar
          Feb 24 '11 at 12:35





          Thanks again. Next time, I'll post a new question instead.

          – Gulbahar
          Feb 24 '11 at 12:35













          while :; do :; done work (: is a synonym for true)

          – Fixee
          Aug 29 '14 at 1:42





          while :; do :; done work (: is a synonym for true)

          – Fixee
          Aug 29 '14 at 1:42













          0














          When you are using the read command in a while loop it need input:



          echo "Hello" | while read line ; do echo $line ; done


          or using several lines:



          echo "Hello" | while read line
          do
          echo $line
          done





          share|improve this answer




























            0














            When you are using the read command in a while loop it need input:



            echo "Hello" | while read line ; do echo $line ; done


            or using several lines:



            echo "Hello" | while read line
            do
            echo $line
            done





            share|improve this answer


























              0












              0








              0







              When you are using the read command in a while loop it need input:



              echo "Hello" | while read line ; do echo $line ; done


              or using several lines:



              echo "Hello" | while read line
              do
              echo $line
              done





              share|improve this answer













              When you are using the read command in a while loop it need input:



              echo "Hello" | while read line ; do echo $line ; done


              or using several lines:



              echo "Hello" | while read line
              do
              echo $line
              done






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 24 '11 at 12:11









              Anders ZommarinAnders Zommarin

              4,62511821




              4,62511821























                  0














                  What are you trying to do? From the look of it it seems that you are trying to read into a variable?



                  This is done by simply stating read the value can then be found inside of $



                  ex:



                  read myvar

                  echo $myvar


                  As other have stated the trouble with the loop is that it is empty which is not allowed.






                  share|improve this answer




























                    0














                    What are you trying to do? From the look of it it seems that you are trying to read into a variable?



                    This is done by simply stating read the value can then be found inside of $



                    ex:



                    read myvar

                    echo $myvar


                    As other have stated the trouble with the loop is that it is empty which is not allowed.






                    share|improve this answer


























                      0












                      0








                      0







                      What are you trying to do? From the look of it it seems that you are trying to read into a variable?



                      This is done by simply stating read the value can then be found inside of $



                      ex:



                      read myvar

                      echo $myvar


                      As other have stated the trouble with the loop is that it is empty which is not allowed.






                      share|improve this answer













                      What are you trying to do? From the look of it it seems that you are trying to read into a variable?



                      This is done by simply stating read the value can then be found inside of $



                      ex:



                      read myvar

                      echo $myvar


                      As other have stated the trouble with the loop is that it is empty which is not allowed.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 24 '11 at 12:12









                      SedrikSedrik

                      1,2481317




                      1,2481317























                          0














                          This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.



                          while
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done


                          When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.



                          while
                          save=$line
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done





                          share|improve this answer


























                          • Dennis, did you mean to init line with something other than ''? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well.

                            – paxdiablo
                            Jul 22 '13 at 22:53













                          • @paxdiablo: You are correct. Other languages have a proper do loop with a test at the end, but Bash does not. My answer here shows how to emulate a do while loop in Bash. I've updated this answer to match that one.

                            – Dennis Williamson
                            Jul 23 '13 at 0:14
















                          0














                          This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.



                          while
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done


                          When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.



                          while
                          save=$line
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done





                          share|improve this answer


























                          • Dennis, did you mean to init line with something other than ''? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well.

                            – paxdiablo
                            Jul 22 '13 at 22:53













                          • @paxdiablo: You are correct. Other languages have a proper do loop with a test at the end, but Bash does not. My answer here shows how to emulate a do while loop in Bash. I've updated this answer to match that one.

                            – Dennis Williamson
                            Jul 23 '13 at 0:14














                          0












                          0








                          0







                          This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.



                          while
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done


                          When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.



                          while
                          save=$line
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done





                          share|improve this answer















                          This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.



                          while
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done


                          When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.



                          while
                          save=$line
                          read -r line
                          [[ $line ]]
                          do
                          :
                          done






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 23 '13 at 0:14

























                          answered Feb 24 '11 at 15:51









                          Dennis WilliamsonDennis Williamson

                          238k63307373




                          238k63307373













                          • Dennis, did you mean to init line with something other than ''? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well.

                            – paxdiablo
                            Jul 22 '13 at 22:53













                          • @paxdiablo: You are correct. Other languages have a proper do loop with a test at the end, but Bash does not. My answer here shows how to emulate a do while loop in Bash. I've updated this answer to match that one.

                            – Dennis Williamson
                            Jul 23 '13 at 0:14



















                          • Dennis, did you mean to init line with something other than ''? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well.

                            – paxdiablo
                            Jul 22 '13 at 22:53













                          • @paxdiablo: You are correct. Other languages have a proper do loop with a test at the end, but Bash does not. My answer here shows how to emulate a do while loop in Bash. I've updated this answer to match that one.

                            – Dennis Williamson
                            Jul 23 '13 at 0:14

















                          Dennis, did you mean to init line with something other than ''? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well.

                          – paxdiablo
                          Jul 22 '13 at 22:53







                          Dennis, did you mean to init line with something other than ''? It appears to me that the loop will never start. My understanding is that until is simply the negative condition version of while in that it doesn't guarantee a minimum execution of one iteration. I may be wrong but the bash manpage seems to indicate that's the case as well.

                          – paxdiablo
                          Jul 22 '13 at 22:53















                          @paxdiablo: You are correct. Other languages have a proper do loop with a test at the end, but Bash does not. My answer here shows how to emulate a do while loop in Bash. I've updated this answer to match that one.

                          – Dennis Williamson
                          Jul 23 '13 at 0:14





                          @paxdiablo: You are correct. Other languages have a proper do loop with a test at the end, but Bash does not. My answer here shows how to emulate a do while loop in Bash. I've updated this answer to match that one.

                          – Dennis Williamson
                          Jul 23 '13 at 0:14











                          0














                          If you are looking to create a menu with choices as a infinite loop (with the choice to break out)



                          PS3='Please enter your choice: '
                          options=("hello" "date" "quit")
                          select opt in "${options[@]}"
                          do
                          case $opt in
                          "hello") echo "world";;
                          "date") echo $(date);;
                          "quit")
                          break;;
                          *) echo "invalid option";;
                          esac
                          done


                          PS3 is described here






                          share|improve this answer




























                            0














                            If you are looking to create a menu with choices as a infinite loop (with the choice to break out)



                            PS3='Please enter your choice: '
                            options=("hello" "date" "quit")
                            select opt in "${options[@]}"
                            do
                            case $opt in
                            "hello") echo "world";;
                            "date") echo $(date);;
                            "quit")
                            break;;
                            *) echo "invalid option";;
                            esac
                            done


                            PS3 is described here






                            share|improve this answer


























                              0












                              0








                              0







                              If you are looking to create a menu with choices as a infinite loop (with the choice to break out)



                              PS3='Please enter your choice: '
                              options=("hello" "date" "quit")
                              select opt in "${options[@]}"
                              do
                              case $opt in
                              "hello") echo "world";;
                              "date") echo $(date);;
                              "quit")
                              break;;
                              *) echo "invalid option";;
                              esac
                              done


                              PS3 is described here






                              share|improve this answer













                              If you are looking to create a menu with choices as a infinite loop (with the choice to break out)



                              PS3='Please enter your choice: '
                              options=("hello" "date" "quit")
                              select opt in "${options[@]}"
                              do
                              case $opt in
                              "hello") echo "world";;
                              "date") echo $(date);;
                              "quit")
                              break;;
                              *) echo "invalid option";;
                              esac
                              done


                              PS3 is described here







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 14 '18 at 22:31









                              BenbentwoBenbentwo

                              1016




                              1016























                                  -1














                                  If you type help while in bash you'll get an explanation of the command.






                                  share|improve this answer




























                                    -1














                                    If you type help while in bash you'll get an explanation of the command.






                                    share|improve this answer


























                                      -1












                                      -1








                                      -1







                                      If you type help while in bash you'll get an explanation of the command.






                                      share|improve this answer













                                      If you type help while in bash you'll get an explanation of the command.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 24 '11 at 12:11









                                      onemasseonemasse

                                      4,41552535




                                      4,41552535






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • 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%2fstackoverflow.com%2fquestions%2f5104426%2fwhile-loop-in-bash-script%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







                                          這個網誌中的熱門文章

                                          Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                          Xamarin.form Move up view when keyboard appear

                                          JBPM : POST request for execute process go wrong