Fix: “Not able to execute a .sh file: /bin/bash^M: bad interpreter”

Bash scripting is an essential part of Linux OS as it is mainly used to automate various tasks on the system. While executing a bash script, you might face an error stating, “Not able to execute a .sh file: /bin/bash^M: bad interpreter?”. The message “^M” is a control character representing a carriage return in Windows that the Linux operating system does not recognize. Let’s dive into more details. 

This post will list the reasons and the solutions to fix the error. “Not able to execute a .sh file: /bin/bash.” 

  • Reason 1: Script Transferred from Windows to Linux
  • Solution 1: Install Utility dos2unix
  • Solution 2: Removing the Carriage Return using the sed Command
  • Problem 2: Use of Incompatible Text Editor
  • Solution: Use indicators like Vim and nano

Reason 1: Script Transferred From Windows to Linux

The most common cause is that the script file was created on a Windows system and transferred to a Linux system without proper conversion. This can result in the Windows carriage return characters being present in the script file, causing the error message.

Solution 1: Install Utility dos2unix

To resolve this error, you can install a Linux utility named “dos2unix”, which converts the Windows-based file to Linux format. The Windows-based file uses carriage return and line feed to break a line, whereas in Linux is represented by the line feed only. So, the additional carriage return is usually responsible for causing this error that can be removed using this utility by installing it first:

$ sudo apt install dos2unix        #For Ubuntu
$ sudo yum install dos2unix        #For CentOS
$ sudo dnf install dos2unix        #For Fedora

After installing this utility, all you need to do is to run the command mentioned below, which will help you resolve your error:

$ dos2unix bashfile.sh

In the above file, bashfile.sh is the file name containing that error.

Solution 2: Removing the Carriage Return Using the sed Command

There is another command that can also be used to remove the carriage return, which is mentioned below:

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

-i: This option is used to edit a file.

-e: tells sed to use the following string as a script to execute.

‘s/\r$//’: which is a regular expression that matches the Windows carriage return character at the end of a line (\r$) and then deletes it from bashfile.sh.

After that, you should try again running the bash script, which should work now:

$ bash bashfile.sh

Reason 2: Use of Incompatible Text Editor

Another cause of this error message is that the script file was edited using a text editor that does not properly handle Linux line endings, such as Notepad. This can result in the line endings being changed from Unix line feed characters to Windows carriage return characters.

Solution: Use Editors Like Vim and nano

Another solution is to use a text editor capable of properly handling Linux line endings. Some popular text editors that can do many are Vim and Nano.

Conclusion

The error message “Not able to execute a .sh file: /bin/bash^M: bad interpreter” is caused by a problem with the line endings in the script file. This error message can be resolved by using dos2unix or by using a text editor that is capable of properly handling Unix line endings.

Following the solutions provided in this article, you can resolve the error message and successfully execute your script files on your Linux system.