Bootstrap FreeKB - Bash (Scripting) - Resolve /bin/bash^M: bad interpreter: No such file or directory
Bash (Scripting) - Resolve /bin/bash^M: bad interpreter: No such file or directory

Updated:   |  Bash (Scripting) articles

This error appears when attempting to run a Bash shell script.

~]# example.sh
-bash: ./example.sh: bin/bash^M: bad interpreter: No such file or directory

 

This typically occurs when a bash script is copied from one machine to another machine, which causes carriage returns to be added to the ends of the lines in the script.

Sometimes, the file command can be used to determine if a file has carriage returns. In this example, CRLF line terminators means the file most likely has carriage returns.

~]# file /path/to/example.py
/path/to/example.py: Python script, ASCII text executable, with CRLF line terminators

 

The following command will remove the carriage returns from the script.

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

 

If the above command works, this may be only a temporary fix, as you would need to do the same the next time the file is moved from a DOS/Windows machine to a Unix/Linux machine. To prevent this problem from continuing, open the script in vi, and set the file format to Unix.

:set fileformat=unix

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 54ca30 in the box below so that we can be sure you are a human.