Bash Read: Reading comma separated list, last element is missed





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







8















The output of the command below is weird to me. Why does it not give me back element 5?



$ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
0
1
2
3
4


I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu). Adding a comma works, but my input data does not have a comma. Am I missing something?










share|improve this question































    8















    The output of the command below is weird to me. Why does it not give me back element 5?



    $ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
    0
    1
    2
    3
    4


    I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu). Adding a comma works, but my input data does not have a comma. Am I missing something?










    share|improve this question



























      8












      8








      8


      2






      The output of the command below is weird to me. Why does it not give me back element 5?



      $ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
      0
      1
      2
      3
      4


      I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu). Adding a comma works, but my input data does not have a comma. Am I missing something?










      share|improve this question
















      The output of the command below is weird to me. Why does it not give me back element 5?



      $ echo '0,1,2,3,4,5' | while read -d, i; do echo $i; done
      0
      1
      2
      3
      4


      I would expect '5' to be returned as well. Running GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu). Adding a comma works, but my input data does not have a comma. Am I missing something?







      bash shell-script read shell-builtin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 17 at 12:18









      terdon

      137k33277458




      137k33277458










      asked May 17 at 10:58









      KarloKarlo

      9882820




      9882820






















          4 Answers
          4






          active

          oldest

          votes


















          12














          With read, -d is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read returns false on EOF and the loop exits (even though the final value was read).



          echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }


          (Even with -d, read also uses $IFS, absorbing whitespace including the trailing n on the final value that would appear using other methods such as readarray)



          The Bash FAQ discusses this, and how to handle various similar cases:




          • Bash Pitfalls #47 IFS=, read [...]

          • BashFAQ 001 How can I read a file [...] line-by-line

          • BashFAQ 005 How can I use array variables?






          share|improve this answer





















          • 8





            I guess one could do read -d, i || [[ -n $i ]] a la What does while read -r line || [[ -n $line ]] mean?

            – steeldriver
            May 17 at 11:18



















          8














          As other answers state, -d is an end-of-line character, not a field separator. You can do



          IFS=, read -a fields <<< "1,2,3,4,5"
          for i in "${fields[@]}"; do echo "$i"; done





          share|improve this answer

































            5














            From man:




            -d delim



            The first character of delim is used to terminate the input line, rather than newline.




            Your element 5 doesn't have a delimiter (comma), so it won't be read.






            share|improve this answer


























            • So the best fix is putting another comma after the input?

              – Karlo
              May 17 at 11:09






            • 3





              The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.

              – Jeff Schaller
              May 17 at 11:15



















            3














            What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?



            As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows



            while read -d, i || [[ -n $i ]]; do ...


            Ex.



            $ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
            0
            1
            2
            3
            4
            5





            share|improve this answer
























              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%2f519471%2fbash-read-reading-comma-separated-list-last-element-is-missed%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              12














              With read, -d is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read returns false on EOF and the loop exits (even though the final value was read).



              echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }


              (Even with -d, read also uses $IFS, absorbing whitespace including the trailing n on the final value that would appear using other methods such as readarray)



              The Bash FAQ discusses this, and how to handle various similar cases:




              • Bash Pitfalls #47 IFS=, read [...]

              • BashFAQ 001 How can I read a file [...] line-by-line

              • BashFAQ 005 How can I use array variables?






              share|improve this answer





















              • 8





                I guess one could do read -d, i || [[ -n $i ]] a la What does while read -r line || [[ -n $line ]] mean?

                – steeldriver
                May 17 at 11:18
















              12














              With read, -d is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read returns false on EOF and the loop exits (even though the final value was read).



              echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }


              (Even with -d, read also uses $IFS, absorbing whitespace including the trailing n on the final value that would appear using other methods such as readarray)



              The Bash FAQ discusses this, and how to handle various similar cases:




              • Bash Pitfalls #47 IFS=, read [...]

              • BashFAQ 001 How can I read a file [...] line-by-line

              • BashFAQ 005 How can I use array variables?






              share|improve this answer





















              • 8





                I guess one could do read -d, i || [[ -n $i ]] a la What does while read -r line || [[ -n $line ]] mean?

                – steeldriver
                May 17 at 11:18














              12












              12








              12







              With read, -d is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read returns false on EOF and the loop exits (even though the final value was read).



              echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }


              (Even with -d, read also uses $IFS, absorbing whitespace including the trailing n on the final value that would appear using other methods such as readarray)



              The Bash FAQ discusses this, and how to handle various similar cases:




              • Bash Pitfalls #47 IFS=, read [...]

              • BashFAQ 001 How can I read a file [...] line-by-line

              • BashFAQ 005 How can I use array variables?






              share|improve this answer















              With read, -d is used to terminate the input lines (i.e. not to separate input lines). Your last "line" contains no terminator, so read returns false on EOF and the loop exits (even though the final value was read).



              echo '0,1,2,3,4,5' | { while read -d, i; do echo "$i"; done; echo "last value=$i"; }


              (Even with -d, read also uses $IFS, absorbing whitespace including the trailing n on the final value that would appear using other methods such as readarray)



              The Bash FAQ discusses this, and how to handle various similar cases:




              • Bash Pitfalls #47 IFS=, read [...]

              • BashFAQ 001 How can I read a file [...] line-by-line

              • BashFAQ 005 How can I use array variables?







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 17 at 17:15

























              answered May 17 at 11:09









              mr.spuraticmr.spuratic

              7,3611229




              7,3611229








              • 8





                I guess one could do read -d, i || [[ -n $i ]] a la What does while read -r line || [[ -n $line ]] mean?

                – steeldriver
                May 17 at 11:18














              • 8





                I guess one could do read -d, i || [[ -n $i ]] a la What does while read -r line || [[ -n $line ]] mean?

                – steeldriver
                May 17 at 11:18








              8




              8





              I guess one could do read -d, i || [[ -n $i ]] a la What does while read -r line || [[ -n $line ]] mean?

              – steeldriver
              May 17 at 11:18





              I guess one could do read -d, i || [[ -n $i ]] a la What does while read -r line || [[ -n $line ]] mean?

              – steeldriver
              May 17 at 11:18













              8














              As other answers state, -d is an end-of-line character, not a field separator. You can do



              IFS=, read -a fields <<< "1,2,3,4,5"
              for i in "${fields[@]}"; do echo "$i"; done





              share|improve this answer






























                8














                As other answers state, -d is an end-of-line character, not a field separator. You can do



                IFS=, read -a fields <<< "1,2,3,4,5"
                for i in "${fields[@]}"; do echo "$i"; done





                share|improve this answer




























                  8












                  8








                  8







                  As other answers state, -d is an end-of-line character, not a field separator. You can do



                  IFS=, read -a fields <<< "1,2,3,4,5"
                  for i in "${fields[@]}"; do echo "$i"; done





                  share|improve this answer















                  As other answers state, -d is an end-of-line character, not a field separator. You can do



                  IFS=, read -a fields <<< "1,2,3,4,5"
                  for i in "${fields[@]}"; do echo "$i"; done






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 17 at 13:12









                  Scott

                  7,24852953




                  7,24852953










                  answered May 17 at 12:51









                  Graham BreedGraham Breed

                  811




                  811























                      5














                      From man:




                      -d delim



                      The first character of delim is used to terminate the input line, rather than newline.




                      Your element 5 doesn't have a delimiter (comma), so it won't be read.






                      share|improve this answer


























                      • So the best fix is putting another comma after the input?

                        – Karlo
                        May 17 at 11:09






                      • 3





                        The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.

                        – Jeff Schaller
                        May 17 at 11:15
















                      5














                      From man:




                      -d delim



                      The first character of delim is used to terminate the input line, rather than newline.




                      Your element 5 doesn't have a delimiter (comma), so it won't be read.






                      share|improve this answer


























                      • So the best fix is putting another comma after the input?

                        – Karlo
                        May 17 at 11:09






                      • 3





                        The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.

                        – Jeff Schaller
                        May 17 at 11:15














                      5












                      5








                      5







                      From man:




                      -d delim



                      The first character of delim is used to terminate the input line, rather than newline.




                      Your element 5 doesn't have a delimiter (comma), so it won't be read.






                      share|improve this answer















                      From man:




                      -d delim



                      The first character of delim is used to terminate the input line, rather than newline.




                      Your element 5 doesn't have a delimiter (comma), so it won't be read.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 17 at 12:18









                      terdon

                      137k33277458




                      137k33277458










                      answered May 17 at 11:09









                      msp9011msp9011

                      5,09044269




                      5,09044269













                      • So the best fix is putting another comma after the input?

                        – Karlo
                        May 17 at 11:09






                      • 3





                        The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.

                        – Jeff Schaller
                        May 17 at 11:15



















                      • So the best fix is putting another comma after the input?

                        – Karlo
                        May 17 at 11:09






                      • 3





                        The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.

                        – Jeff Schaller
                        May 17 at 11:15

















                      So the best fix is putting another comma after the input?

                      – Karlo
                      May 17 at 11:09





                      So the best fix is putting another comma after the input?

                      – Karlo
                      May 17 at 11:09




                      3




                      3





                      The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.

                      – Jeff Schaller
                      May 17 at 11:15





                      The best fix might be to process data with something other than a shell. Since the answerers here have addressed the current question, you might consider a separate question that demonstrates your larger goal.

                      – Jeff Schaller
                      May 17 at 11:15











                      3














                      What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?



                      As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows



                      while read -d, i || [[ -n $i ]]; do ...


                      Ex.



                      $ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
                      0
                      1
                      2
                      3
                      4
                      5





                      share|improve this answer




























                        3














                        What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?



                        As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows



                        while read -d, i || [[ -n $i ]]; do ...


                        Ex.



                        $ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
                        0
                        1
                        2
                        3
                        4
                        5





                        share|improve this answer


























                          3












                          3








                          3







                          What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?



                          As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows



                          while read -d, i || [[ -n $i ]]; do ...


                          Ex.



                          $ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
                          0
                          1
                          2
                          3
                          4
                          5





                          share|improve this answer













                          What you're seeing is the same behavior (and for the same reason) as Why does this 'while' loop not recognize the last line?



                          As in that case, you can modify the behavior by adding an extra test to the loop termination condition, as follows



                          while read -d, i || [[ -n $i ]]; do ...


                          Ex.



                          $ echo '0,1,2,3,4,5' | while read -d, i || [[ -n $i ]]; do echo $i; done
                          0
                          1
                          2
                          3
                          4
                          5






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 17 at 13:24









                          steeldriversteeldriver

                          39.9k45493




                          39.9k45493






























                              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%2f519471%2fbash-read-reading-comma-separated-list-last-element-is-missed%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

                              He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

                              Bunad

                              Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum