How to Solve Bad Interpreter Error in Bash

Yahya Irmak Feb 02, 2024
  1. Solve the /bin/bash^M: bad interpreter Error in Bash
  2. Change EOL Conversion in Windows to Solve the /bin/bash^M: bad interpreter Error in Bash
  3. Use the dos2unix Command to Solve the /bin/bash^M: bad interpreter Error in Bash
  4. Use the sed Command to Solve the /bin/bash^M: bad interpreter Error in Bash
How to Solve Bad Interpreter Error in Bash

The line endings of files created in Unix/Linux operating systems and files created in DOS/Windows operating systems differ. This difference may cause a file created in one operating system not to be executed in another.

This article explains how to solve one of these errors, the /bin/bash^M: bad interpreter: No such file or directory error in Linux Bash.

Solve the /bin/bash^M: bad interpreter Error in Bash

Unix operating systems use line feed ("\n") as the end of the line. However, Windows operating systems use carriage return and line feed ("\r\n").

So you cannot run a bash script written in Windows on Unix. You must clear the carriage return characters to execute the file.

The rest of the article explains different ways to solve this error.

Change EOL Conversion in Windows to Solve the /bin/bash^M: bad interpreter Error in Bash

Text editors on Windows set line endings to CRLF by default. Change it to LF in the settings of the application you are using.

For example:

  1. In Sublime Text, you can select the Unix in the Line Endings settings on the View tab.
  2. In Notepad++, you can select the Unix (LF) in the EOL Conversion settings on the Edit tab.

eol conversion

You can execute this file on Unix operating systems. It will not give errors anymore.

Use the dos2unix Command to Solve the /bin/bash^M: bad interpreter Error in Bash

The doc2unix command-line tool is a DOS to Unix text file format converter and vice versa. We can use this tool to make our file Unix-compatible.

Its usage is as follows.

dos2unix test.sh

The file will be converted to Unix format. You can now execute the file.

./file.sh

Use the sed Command to Solve the /bin/bash^M: bad interpreter Error in Bash

The sed command-line tool performs text transformations on an input stream. You can remove the "\r" characters in the file with the command below.

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

Now you can execute the file without error.

./file.sh

sed

Besides all these methods, you can rewrite the file content with a text editor in Unix systems. This way, the line endings are set correctly, and you do not get an error.

Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn

Related Article - Bash Error