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

Bash bad interpreter

If you have problem when execute bash script, it’s caused by Windows environment and then porting over to run on a Unix environment.

If you use Notepad++, go to Edit and EOL Conversion and choose UNIX/OSX Format.

Sublime Text fix bash script bad interpreter
Notepad++ fix bash script bad interpreter

If you use Sublime text, open menu View, Line Endings and choose Unix.

Sublime Text fix bash script bad interpreter
Sublime Text fix bash script bad interpreter

Or you can running dos2unix on the script:

http://dos2unix.sourceforge.net/

Or just edit the script in your Unix environment using vi and test, but sometimes can’t work, for exampe if you use Putty to connect Unix environment.

Unix uses different line endings so can’t read the file you created on Windows. Hence it is seeing ^M as an illegal character.

If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.

Below is simple bash script to make it works:

# sed -i -e 's/\r$//' script.sh

ServerDiary

ServerDiary

Leave a Reply

Your email address will not be published. Required fields are marked *