Fixing /bin/bash^M no such file or directory errors when running shell scripts




Computers are like Old Testament gods; lots of rules and no mercy.
-- Joseph Campbell
Joseph Campbell and the Power of Myth with Bill Moyers (video) (ep. 2 chapt. 22)




I had reason to rerun a shell script that created a Linux Box on Vagrant / Virtual Box on my MacBook. Running it resulted in an error: 

zsh: ./vagrant-linux.sh: bad interpreter: /bin/bash^M: no such file or directory

zsh: ./vagrant-linux.sh: bad interpreter: /bin/bash^M: no such file or directory 



This error is helpful because it was easily Googleable and had a number of suggestions on how to fix it.

In the time honored tradition of our people (anyone who codes) I found myself on a Stackoverflow thread

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

It turns out that this (as explained in the Stackoverflow thread) is because Windows adds a carriage return character in addition \n at the end of lines. The sed command removes the carriage return character by substituting \r for an employ string using the Linux stream edit command (sed).

Computers have a lot of rules, and no mercy. 



Comments