others-how to solve '/bin/bash^M: bad interpreter: No such file or directory'

1. Purpose

Sometimes, when we execute a bash script in linux , we got this error:

-bash: ./start_server.sh: /bin/bash^M: bad interpreter: No such file or directory

2. The solution

The script file is in DOS format, e.g., the end of each line is identified by \r\n. Use the vim editor to open the script and run:

:set ff?

You can see the words DOS or UNIX.

2.1 Solution #1

We can use vim to change the file from windows format to unix format. You can input the following command in vim:

:set ff=unix

or

:set fileformat=unix

to force it to be in unix format, then save it (:wq) and exit.

2.2 Solution #2

We can also use the sed command to replace the \r with empty as follows:

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

sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

We use the -i option to edit files in-place instead of printing to standard output.

We use the -e option to specify a script.

Here is an example script to replace hello with world is as follows:

 sed -i -e 's/hello/world/' file.txt

The $ is a special character in linux regex matching:

Symbol Descriptions
. replaces any character
^ matches start of string
$ matches end of string
* matches up zero or more times the preceding character
\ Represent special characters
() Groups regular expressions
? Matches up exactly one character

2.3 Solution #3

For mac OS/Linux users, you can try this solution:

You can also use gedit to remove the unwanted characters. Under the File menu select Save As and set the line end type unix/Linux.

gedit is the text editor of the GNOME desktop environment. Its name means Really good text Editor For Linux;

The first goal of gedit is to be easy to use, with a simple interface by default. More advanced features are available by enabling plugins.

2.4 Solution #4

For ubuntu users, you can try this solution:

sudo apt-get install tofrodos
fromdos file

Tofrodos is a text file conversion utility that converts ASCII and Unicode UTF-8 files between the MSDOS (or Windows) format, which traditionally have CR/LF (carriage return/line feed) pairs as their new line delimiters, and the Unix format, which usually have LFs (line feeds) to terminate each line.

It is a useful utility to have around when you have to convert files between MSDOS (or Windows) and Unix/Linux/BSD (and her clones and variants). It comes standard with a number of systems and is often found on the system as “todos”, “fromdos”, “dos2unix” and “unix2dos”.

3. Summary

In this post, I demonstrated how to solve the /bin/bash^M: bad interpreter: No such file or directory problem in linux system, the key point is to use unix format instead of windows format. That’s it, thanks for your reading.

-->