No Such File or Directory” Error in Linux

Person typing on the Cherry KW 9200 Mini Keyboard
Hannah Stryker / How-To Geek
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 the dos2unix command, 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.

What Is the “Bad Interpreter: No Such File or Directory” Error?

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 wrong line endings—the characters placed at the end of a line indicating it’s time to move to the next line. DOS-descended Windows likes 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.

A "bad interpreter: no such file or directory" error message in a Linux command line.

You can even confirm the problem in a Linux terminal by pointing the file command to your script.

file script1.sh

Check a file's line endings with the file command in Linux.

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.

How to Fix a “Bad Interpreter: No Such File or Directory” Error

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 named dos2unix. It’s available in most default repositories, so you can install it on Ubuntu with:

sudo apt install dos2unix

On our Fedora installation dos2unix came preinstalled, but you can confirm you have it with:

sudo dnf install dos2unix

On Arch Linux:

sudo pacman -S dos2unix

Using dos2unix is simple; just give it your file name.

dos2unix script1.sh

Enter the dos2unix command.

Check it with file if 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.

dos2unix script1.sh script2.sh script3.sh

Or, if you have consistent file naming, you can of course write shorter commands using wildcards.

dos2unix script*.sh

Use an asterisk in your dos2unix command to convert with a wildcard.

The dos2unix command has several flags to help you make special kinds of conversions, like changing file ownership. You can even use it in reverse form, unix2dos, if you want to switch back to CRLF. Enter dos2unix --help to learn more.

Using the tr Command

If you can’t or don’t want to install a dedicated utility, Linux has built-in tools that will clean up those newlines. With the tr command, you can strip out the \r part of the line endings so we’re left with \n terminators.

tr -d '\r' < script1.sh > script1_unix.sh

Here, tr is taking the text of the script1.sh file, deleting every instance of \r it finds, and saving the output as the file script1_unix.sh.

Using the sed Command

The mighty sed command built into your shell can also change line endings for your file.

sed -i 's/\r//' script1.sh

If you aren’t familiar with sed syntax, we’re telling sed to edit our file (-i) and substitute (s/) each carriage return (\r) with nothing. That leaves us with Unix’s preferred \n line 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.

:set fileformat=unix

Change the script file format with a vim command.

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 and IDE out there has a toggle for line endings. That includes Geany.

To convert line endings in Geany, go to Document > Set Line Endings > Convert and Set to LF (Unix).

In Geany, under the Document menu, go to "Set Line Endings" and choose "Convert and Set to LF (Unix)".

Save your script and try running it again.

Using Kate or Kwrite

If you use Kwrite to edit your script, or Kwrite’s more powerful cousin Kate, you can convert to LF format by clicking Tools > End of Line > Unix.

In Kwrite, in the Tools menu open "End of Line" and select "Unix."

Save the file and run your script again.

Using Notepad++

If you edit code in Notepad++ you can also easily convert line endings, which is handy because you’re likely running it on Windows (unless you’ve installed 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 typing eol in the settings search bar. The top result should be a drop-down menu for setting the “Eol”. Change it to “\n”.

In VS Code settings, change the default EOL setting to "\n".

RELATED: How to Make Your Bash Scripts Hardware Aware

Source

      Guidantech
      Logo
      Shopping cart