Wednesday 2 November 2011

/bin/sh^M: bad interpreter: No such file or directory

I got the following error when attempting to run a .sh file I had obtained;

bash: ./thefile.sh: /bin/sh^M: bad interpreter: No such file or directory

After a little bit of research it turns out that the file had been written on Windows and then exported thus creating a basic text format issue.

Under DOS (Windows/PC) the end of a line of text is signalled using the ASCII code sequence CarriageReturn,LineFeed (alternately written as CR,LF or the bytes 0×0D,0×0A). On the Macintosh platform, only the CR character is used. Under UNIX, the opposite is true and only the LF character is used.

To fix this I ran the following command which converts the file to UNIX format;

dos2unix thefile.sh

Information on the dos2unix command can be found here.