Summary
If you get a “bad interpreter: no such file or directory” error when running a shell script, you need to convert your script file from Windows to Unix line endings. One method is to use thedos2unixcommand, like this:dos2unix script1.sh
Did you try running a Bash script and got a “/bin/bash^M: bad interpreter: No such file or directory” error message? The output isn’t very helpful and almost gives the impression your script is missing. Fortunately, that’s not the case, and the fix is very simple.

Getting an error message that reads “/bin/bash^M: bad interpreter: No such file or directory” just means that your shell script file has the wrongline endings—the characters placed at the end of a line indicating it’s time to move to the next line.DOS-descended Windowslikes to see a “carriage return and line feed” (CRLF) terminator, notated “\r\n”. In contrast, Linux, modern macOS, and other Unix-based systems use just “line feed” (LF), or\n.
If you’re seeing this bad interpreter error, chances are either you or the person you got the script from originally wrote it on a Windows computer. Bash sees those \r\n line endings set by the Windows computer and gets upset because they prevent the shell from properly interpreting your script.

You can even confirm the problem in a Linux terminal by pointing thefilecommand to your script.
If the file uses DOS line endings, you’ll see the message “with CRLF line terminators” appended in the output. Had our script been using the proper Unix (LF) newlines, it wouldn’t mention terminators at all. Fortunately, getting back in LF business is easy.

All it takes to get rid of that error message and start running your scripts as normal is switching from DOS line endings to Unix line endings. You can do this with terminal commands or in your favorite code editor. Here are eight ways to fix it:
Using the dos2unix Command
There’s a command-line program made just for converting DOS (aka, Windows) files into fully Unix-compatible files aptly nameddos2unix. It’s available in most default repositories, so you can install it on Ubuntu with:
On ourFedorainstallationdos2unixcame preinstalled, but you may confirm you have it with:

Usingdos2unixis simple; just give it your file name.
Check it withfileif you want to confirm the conversion was successful before running your script. You also can mass-convert by naming multiple files separated by only a space.
Or, if you have consistent file naming, you can of course write shorter commands using wildcards.

Thedos2unixcommand has several flags to help you make special kinds of conversions, likechanging file ownership. You can even use it in reverse form,unix2dos, if you want to switch back to CRLF. Enterdos2unix –helpto learn more.
Using the tr Command
If you’re able to’t or don’t want to install a dedicated utility, Linux has built-in tools that will clean up those newlines. Withthetrcommand, you can strip out the\rpart of the line endings so we’re left with\nterminators.
Here,tris taking the text of thescript1.shfile, deleting every instance of\rit finds, and saving the output as the filescript1_unix.sh.

Using the sed Command
The mightysedcommandbuilt into your shell can also change line endings for your file.
If you aren’t familiar withsedsyntax, we’re tellingsedto edit our file (-i) and substitute (s/) each carriage return (\r) with nothing. That leaves us with Unix’s preferred\nline terminators.
Using vi or vim
If you use vi or vim to edit your scripts, just pass this command to convert the currently open file to Unix line endings.
Related:How to Exit the Vi or Vim Editor
Using Geany
If you’re working in a desktop environment, there’s no reason to fool around in the terminal just to make your files ready for the Unix world. Virtually every code editor andIDEout there has a toggle for line endings. That includesGeany.
To convert line endings in Geany, go to Document > Set Line Endings > Convert and Set to LF (Unix).
Save your script and try running it again.
Using Kate or Kwrite
If you useKwriteto edit your script, or Kwrite’s more powerful cousinKate, you can convert to LF format by clicking Tools > End of Line > Unix.
Save the file and run your script again.
Using Notepad++
If you edit code in Notepad++ you’re able to also easily convert line endings, which is handy because you’re likely running it on Windows (unless you’veinstalled Notepad++ for Linux) and can convert them before moving to your Linux system. With the file open, go to Edit > EOL Conversion > Unix (LF).
Be sure to save your script before running it.
If you want Notepad++ to create files with Unix line endings by default, go to Settings > Preferences, select the “New Document” tab, and under the “Format (line ending)” options click the “Unix (LF)” radio button.
Using VS Code
Visual Studio Code (VS Code) works much the same way, only the toggle is even easier to find. Just click “CRLF” at the bottom-right corner of VS Code’s window.
You can also set VS Code to use Unix line endings by default by going to File > Preferences > Settings and typingeolin the settings search bar. The top result should be a drop-down menu for setting the “Eol”. Change it to “\n”.