Replace unix 'new line' with windows 'new line'

I'm trying to write a script to convert a unix text file to windows/dos format. Essentially all I want to do is replace the Unix return with a windows one.

on the command line I can do this successfully by:

sed s/$/^M/ ~/unix.txt ~/dos.txt

when I put this line in my shell script it literally enters a ^M in the text file.

Unfortunately you cannot do it that way.

From SED oneliners

normally i use, dos2unix, unix2dos commands for those conversions.

Alternatively, you could use Perl -

perl -i.bak -pe 's/\n/\r\n/g' unix.txt

tyler_durden

You can generate the carriage return character in Shell:

CR=`echo "\0015"`          # Carriage Return
sed -e "s/$/${CR}/g" ~unix.txt > ~dos.txt
awk '{printf("%s\r\n",$0)}' unix_file > dosfile