How to Fix a “Bad Interpreter” Error in a Linux Shell Script

If you opened & saved a shell script file in Windows, this will usually cause it to no longer run in Linux. Instead, you see an error like this:

^M: bad interpreter: No such file or directory

It may just LOOK like a generic unformatted text file, but Windows saves & formats text files differently than Linux, which can cause problems. To fix it, open the file on the Linux machine in vim (e.g., “vim /path/to/file”), hit ESC to be sure you’re in command mode, and then type exactly this:

:set fileformat=unix

…then save it by writing the changes and quitting by typing:

:wq

And that’s it!  Now your script will run in Linux again.

Steve