How to Solve Bad Interpreter Error in Bash
-
Solve the
/bin/bash^M: bad interpreter
Error in Bash -
Change EOL Conversion in Windows to Solve the
/bin/bash^M: bad interpreter
Error in Bash -
Use the
dos2unix
Command to Solve the/bin/bash^M: bad interpreter
Error in Bash -
Use the
sed
Command to Solve the/bin/bash^M: 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:
- In
Sublime Text
, you can select theUnix
in theLine Endings
settings on theView
tab. - In
Notepad++
, you can select theUnix (LF)
in theEOL Conversion
settings on theEdit
tab.
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
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.
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn