Converting DOS files to UNIX

Converting DOS files to UNIX

You might occasionally run a bash script on the HPC and get this error:

$ ./my-bash-script.sh
/bin/bash^M: bad interpreter: No such file or directory

This is because Microsoft Windows uses the two-character sequence of “Carriage Return” and “Line Feed” (CR LF) to mark the end of a line. All UNIX and Linux systems use just a “line feed character” (LF) to mark the end of a line.

Your bash script file has the Windows line endings, so when you run the script it’s looking for the shell interpreter /bin/bash^M and there is no such file. The CR LF shows as ^M in your terminal. The correct shell interpreter is just /bin/bash.

The way to fix this is to run the program dos2unix over your script. That will convert all the CR LF sequences at the end of the lines to just LF.

$ dos2unix my-bash-script.sh
dos2unix: converting file my-bash-script.sh to Unix format...
$

It will now run fine.

This is custom footer