I have below script:

cat list.txt | while IFS='' read -r a; 
#Remove all illegal characters
   a=${a//  /_}
   a=${a// /_}
   echo $b

The expected results should be:


The real results are:


It looks like bash is using .csv to replace the first 4 characters in $filepath.

How to generate the expected results?

  • 2
    You have Windows-style CRLF line endings in the file. The CR gets left in, before you add the .csv and it moves the cursor to the beginning of line when printing.
    – ilkkachu
    Jan 19, 2018 at 17:35
  • @ilkkachu I have changed to Linux line end (LF) and still got the same results. Jan 19, 2018 at 17:41
  • @lovechillcool, did you remove the CRs from both the list.txt file and your script file? Jan 19, 2018 at 17:42
  • 1
    @glennjackman, it's not the script, a CR at the end where, say the do keyword was expected, would cause a syntax error (the shell would see do\r instead)
    – ilkkachu
    Jan 19, 2018 at 17:47
  • 1
    Kindly post list.txt content Jan 19, 2018 at 17:50

1 Answer 1


I have CRLF not only in the script but also in the list.txt. After I changed to LF in both shell and list.txt, the issue is resolved.

Not the answer you're looking for? Browse other questions tagged or ask your own question.