/bin/bash^M: bad interpreter: No such file or directory

Sometime when working with shell script, we need to edit the shell script on Windows environment. Then when we execute the shell script on Linux environment, we will find the below error:

/bin/bash^M: bad interpreter: No such file or directory
/bin/bash^M: bad interpreter: そのようなファイルやディレクトリはありません

Since the new line code had been saved in the \ r \ n, there is a need to fix in the Linux \ n. This is because the new line code had been saved in the \ r \ n on Windows environment. In order to fix the problem, we need to convert the \r\n to \n on Linux environment. To do this, we may use the below command:

$ sed -i 's/\r//' [shell script fileName]

Below, verification

By using the command cat -e, we can see that there is additional \ r at the newline code.

$ cat -e hello.sh
#!/bin/bash^M$
echo "hello,world"^M$
This remains to run an error
$ ./hello.sh
-bash: ./hello.sh: /bin/bash^M: bad interpreter: No such file or directory
Convert line feed code by using sed command
$ sed -i 's/\r//' hello.sh
once again check with cat -e
$ cat -e hello.sh
#!/bin/bash$
echo "hello,world"$
Then, you are done!
$ ./hello.sh
hello,world

One response to this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: