Vivek Iyer Vivek Iyer - 13 days ago 9
Bash Question

Unix script appends ^M at end of each line

I have a Unix shell script which does the following:


  1. creates a backup of a file

  2. appends some text to a file



Now in #2 if I insert a text, ^M gets appended on all the lines of the file.

For example:

echo " a" >> /cust/vivek.txt
echo " b" >> /cust/vivek.txt

vi vivek.txt
abc^M
bcd^M
a^M
b^M


Any way to avoid this?

Thanks,

Vivek

Answer

I'm not sure how echo could be producing ^M characters but you can remove them by running dos2unix on your file, like this:

dos2unix /cust/vivek.txt