Frequently Asked Question
Why does my shell script have ^M everywhere, and what is dos2unix?
Windows ends lines with two characters, a carriage return and a line feed,
\r\n (CRLF), while Unix uses a single line feed, \n (LF). When a file
created on Windows lands on a Linux machine, every line ends with an extra
\r that the shell does not strip. In a script this is fatal: the interpreter
tries to execute commands like bash\r and complains it cannot find them.
Tools that display \r as ^M (such as cat -v or vi) make the problem
visible.
dos2unix file.sh rewrites the file in place, stripping the carriage returns.
unix2dos goes the other way. If dos2unix is not installed, tr -d '\r' < win.sh > unix.sh or sed -i 's/\r$//' file does the same job. To convert
encodings (not just line endings), use iconv -f WINDOWS-1252 -t UTF-8 file.txt > file.utf8.txt. Configuring Git to handle line-endings automatically
(core.autocrlf=input on Linux, true on Windows) prevents the problem at
source for version-controlled files.