Bash reading line by line. Unable to combine variable and string, variable is appended









up vote
0
down vote

favorite












I'm getting some very strange behaviour that I can't figure out when trying to read a txt file.



I have a txt file that looks like this:



98040520
98041022
98041024
...


And then I have a bash script with some basic debugging that looks like this:



#!/usr/bin/env bash
while read id; do
test=123
echo "$test.png"
echo $id
echo "$id.png"
echo "test$id.png"
exit
done <myfile.txt


And the output is



123.png
98040520
.png
.png98040520


What on earth is going on?



It prints "98040520" just fine, but it won't print "98040520.png", it just prints the ".png" part and an attempt to print "test98040520.png" results in the id being appended creating ".png98040520" and the word "test" excluded altogether!!



I figure it's got to be something to do with line endings (the txt file comes from windows) but I haven't been able to find anything online that will help. I've been searching for how to trim whitespace or line endings which may mean i've been looking for the wrong thing.



In case it's not clear what i'd like to do is get "98040520.png" or more specifically filepath=../../path/to/my/dir/98040520.png but what i've posted above is a simpler example of the problem I seem to be having.










share|improve this question

























    up vote
    0
    down vote

    favorite












    I'm getting some very strange behaviour that I can't figure out when trying to read a txt file.



    I have a txt file that looks like this:



    98040520
    98041022
    98041024
    ...


    And then I have a bash script with some basic debugging that looks like this:



    #!/usr/bin/env bash
    while read id; do
    test=123
    echo "$test.png"
    echo $id
    echo "$id.png"
    echo "test$id.png"
    exit
    done <myfile.txt


    And the output is



    123.png
    98040520
    .png
    .png98040520


    What on earth is going on?



    It prints "98040520" just fine, but it won't print "98040520.png", it just prints the ".png" part and an attempt to print "test98040520.png" results in the id being appended creating ".png98040520" and the word "test" excluded altogether!!



    I figure it's got to be something to do with line endings (the txt file comes from windows) but I haven't been able to find anything online that will help. I've been searching for how to trim whitespace or line endings which may mean i've been looking for the wrong thing.



    In case it's not clear what i'd like to do is get "98040520.png" or more specifically filepath=../../path/to/my/dir/98040520.png but what i've posted above is a simpler example of the problem I seem to be having.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm getting some very strange behaviour that I can't figure out when trying to read a txt file.



      I have a txt file that looks like this:



      98040520
      98041022
      98041024
      ...


      And then I have a bash script with some basic debugging that looks like this:



      #!/usr/bin/env bash
      while read id; do
      test=123
      echo "$test.png"
      echo $id
      echo "$id.png"
      echo "test$id.png"
      exit
      done <myfile.txt


      And the output is



      123.png
      98040520
      .png
      .png98040520


      What on earth is going on?



      It prints "98040520" just fine, but it won't print "98040520.png", it just prints the ".png" part and an attempt to print "test98040520.png" results in the id being appended creating ".png98040520" and the word "test" excluded altogether!!



      I figure it's got to be something to do with line endings (the txt file comes from windows) but I haven't been able to find anything online that will help. I've been searching for how to trim whitespace or line endings which may mean i've been looking for the wrong thing.



      In case it's not clear what i'd like to do is get "98040520.png" or more specifically filepath=../../path/to/my/dir/98040520.png but what i've posted above is a simpler example of the problem I seem to be having.










      share|improve this question













      I'm getting some very strange behaviour that I can't figure out when trying to read a txt file.



      I have a txt file that looks like this:



      98040520
      98041022
      98041024
      ...


      And then I have a bash script with some basic debugging that looks like this:



      #!/usr/bin/env bash
      while read id; do
      test=123
      echo "$test.png"
      echo $id
      echo "$id.png"
      echo "test$id.png"
      exit
      done <myfile.txt


      And the output is



      123.png
      98040520
      .png
      .png98040520


      What on earth is going on?



      It prints "98040520" just fine, but it won't print "98040520.png", it just prints the ".png" part and an attempt to print "test98040520.png" results in the id being appended creating ".png98040520" and the word "test" excluded altogether!!



      I figure it's got to be something to do with line endings (the txt file comes from windows) but I haven't been able to find anything online that will help. I've been searching for how to trim whitespace or line endings which may mean i've been looking for the wrong thing.



      In case it's not clear what i'd like to do is get "98040520.png" or more specifically filepath=../../path/to/my/dir/98040520.png but what i've posted above is a simpler example of the problem I seem to be having.







      bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 1:23









      John Mellor

      1,04872148




      1,04872148






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          It's indeed a line-endings issue (see here for an explanation of what the r is doing to your terminal output).



          You can either fix the endings in the file or fix them in the script (tr -d "r" will remove all instances of r to yield unix-style line endings in the text passed to the while loop):



          #!/usr/bin/env bash
          cat myfile.txt | tr -d "r" | while read id; do
          test=123
          echo "$test.png"
          echo $id
          echo "$id.png"
          echo "test$id.png"
          exit
          done


          Using the fixed version gives the expected output.






          share|improve this answer






















          • Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "r" does?
            – John Mellor
            Nov 12 at 1:37










          • I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
            – Ollin Boer Bohan
            Nov 12 at 1:40

















          up vote
          -1
          down vote













          All your quotes and curl brackets are not necessary. Also you have exit in your while loop, you should get rid of that:



          while read id; do
          test=123
          echo $test.png
          echo $id
          echo $id.png
          echo test$id.png
          done <myfile.txt





          share|improve this answer




















          • The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
            – John Mellor
            Nov 12 at 1:33










          • my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png
            – Rico Chen
            Nov 12 at 1:35











          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',
          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%2f53254910%2fbash-reading-line-by-line-unable-to-combine-variable-and-string-variable-is-ap%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








          up vote
          2
          down vote



          accepted










          It's indeed a line-endings issue (see here for an explanation of what the r is doing to your terminal output).



          You can either fix the endings in the file or fix them in the script (tr -d "r" will remove all instances of r to yield unix-style line endings in the text passed to the while loop):



          #!/usr/bin/env bash
          cat myfile.txt | tr -d "r" | while read id; do
          test=123
          echo "$test.png"
          echo $id
          echo "$id.png"
          echo "test$id.png"
          exit
          done


          Using the fixed version gives the expected output.






          share|improve this answer






















          • Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "r" does?
            – John Mellor
            Nov 12 at 1:37










          • I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
            – Ollin Boer Bohan
            Nov 12 at 1:40














          up vote
          2
          down vote



          accepted










          It's indeed a line-endings issue (see here for an explanation of what the r is doing to your terminal output).



          You can either fix the endings in the file or fix them in the script (tr -d "r" will remove all instances of r to yield unix-style line endings in the text passed to the while loop):



          #!/usr/bin/env bash
          cat myfile.txt | tr -d "r" | while read id; do
          test=123
          echo "$test.png"
          echo $id
          echo "$id.png"
          echo "test$id.png"
          exit
          done


          Using the fixed version gives the expected output.






          share|improve this answer






















          • Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "r" does?
            – John Mellor
            Nov 12 at 1:37










          • I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
            – Ollin Boer Bohan
            Nov 12 at 1:40












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          It's indeed a line-endings issue (see here for an explanation of what the r is doing to your terminal output).



          You can either fix the endings in the file or fix them in the script (tr -d "r" will remove all instances of r to yield unix-style line endings in the text passed to the while loop):



          #!/usr/bin/env bash
          cat myfile.txt | tr -d "r" | while read id; do
          test=123
          echo "$test.png"
          echo $id
          echo "$id.png"
          echo "test$id.png"
          exit
          done


          Using the fixed version gives the expected output.






          share|improve this answer














          It's indeed a line-endings issue (see here for an explanation of what the r is doing to your terminal output).



          You can either fix the endings in the file or fix them in the script (tr -d "r" will remove all instances of r to yield unix-style line endings in the text passed to the while loop):



          #!/usr/bin/env bash
          cat myfile.txt | tr -d "r" | while read id; do
          test=123
          echo "$test.png"
          echo $id
          echo "$id.png"
          echo "test$id.png"
          exit
          done


          Using the fixed version gives the expected output.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 1:37

























          answered Nov 12 at 1:34









          Ollin Boer Bohan

          1,365310




          1,365310











          • Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "r" does?
            – John Mellor
            Nov 12 at 1:37










          • I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
            – Ollin Boer Bohan
            Nov 12 at 1:40
















          • Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "r" does?
            – John Mellor
            Nov 12 at 1:37










          • I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
            – Ollin Boer Bohan
            Nov 12 at 1:40















          Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "r" does?
          – John Mellor
          Nov 12 at 1:37




          Thanks that does fix it, none of the other similar solutions worked for me. Would you mind clarifying what tr -d "r" does?
          – John Mellor
          Nov 12 at 1:37












          I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
          – Ollin Boer Bohan
          Nov 12 at 1:40




          I've added an explanation (it's removing the carriage returns so that you get UNIX style line-endings).
          – Ollin Boer Bohan
          Nov 12 at 1:40












          up vote
          -1
          down vote













          All your quotes and curl brackets are not necessary. Also you have exit in your while loop, you should get rid of that:



          while read id; do
          test=123
          echo $test.png
          echo $id
          echo $id.png
          echo test$id.png
          done <myfile.txt





          share|improve this answer




















          • The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
            – John Mellor
            Nov 12 at 1:33










          • my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png
            – Rico Chen
            Nov 12 at 1:35















          up vote
          -1
          down vote













          All your quotes and curl brackets are not necessary. Also you have exit in your while loop, you should get rid of that:



          while read id; do
          test=123
          echo $test.png
          echo $id
          echo $id.png
          echo test$id.png
          done <myfile.txt





          share|improve this answer




















          • The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
            – John Mellor
            Nov 12 at 1:33










          • my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png
            – Rico Chen
            Nov 12 at 1:35













          up vote
          -1
          down vote










          up vote
          -1
          down vote









          All your quotes and curl brackets are not necessary. Also you have exit in your while loop, you should get rid of that:



          while read id; do
          test=123
          echo $test.png
          echo $id
          echo $id.png
          echo test$id.png
          done <myfile.txt





          share|improve this answer












          All your quotes and curl brackets are not necessary. Also you have exit in your while loop, you should get rid of that:



          while read id; do
          test=123
          echo $test.png
          echo $id
          echo $id.png
          echo test$id.png
          done <myfile.txt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 1:30









          Rico Chen

          68349




          68349











          • The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
            – John Mellor
            Nov 12 at 1:33










          • my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png
            – Rico Chen
            Nov 12 at 1:35

















          • The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
            – John Mellor
            Nov 12 at 1:33










          • my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png
            – Rico Chen
            Nov 12 at 1:35
















          The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
          – John Mellor
          Nov 12 at 1:33




          The curly brackets were just me trying to make it clearer, the same output will and does happen without it. And the exit is just because obviously I don't need to loop through them all to demonstrate the issue. Unfortunately your answer doesn't respond to the actual issue i'm having.
          – John Mellor
          Nov 12 at 1:33












          my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png
          – Rico Chen
          Nov 12 at 1:35





          my code produces the following result, isn't that what you expected? 123.png 98040520 98040520.png test98040520.png 123.png 98041022 98041022.png test98041022.png 123.png 98041024 98041024.png test98041024.png
          – Rico Chen
          Nov 12 at 1:35


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53254910%2fbash-reading-line-by-line-unable-to-combine-variable-and-string-variable-is-ap%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







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands