Linux

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

If you have problem when execute bash script, it’s caused by Windows environment and then porting over to run on a Unix environment.

If you use Notepad++, go to Edit and EOL Conversion and choose UNIX/OSX Format.

Notepad++ fix bash script bad interpreter

If you use Sublime text, open menu View, Line Endings and choose Unix.

Sublime Text fix bash script bad interpreter

Or you can running dos2unix on the script:

http://dos2unix.sourceforge.net/

Or just edit the script in your Unix environment using vi and test, but sometimes can’t work, for exampe if you use Putty to connect Unix environment.

Unix uses different line endings so can’t read the file you created on Windows. Hence it is seeing ^M as an illegal character.

If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.

Below is simple bash script to make it works:

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

Recent Posts

How to fix yum update error thread.error: can’t start new thread

If you found error thread.error: can't start new thread on yum update command on CentOS…

5 months ago

How to securing Cockpit login with Google Two Factor Authenticator 2FA

Cockpit is a web-based graphical interface for servers, intended for everyone, especially those who are:…

8 months ago

How to install Cockpit on CentOS 7 / CentOS 9 Stream and configure Nginx reserve proxy

From cockpit-project.org, Cockpit is a web-based graphical interface for servers, intended for everyone, especially those…

10 months ago

How to install and configure Nginx with HTTP3 on CentOS 9 Stream / RHEL 9

We have been using Nginx with HTTP3 for more than 1 year on our production…

11 months ago

How to sync date time using Crony on CentOS 9 Stream / RHEL 9

On CentOS 7, to sync date time we often use NTPD. But on CentOS 9,…

11 months ago

How to install and enable REMI repository on CentOS 9 Stream

Remi repository is one of third-party repository that have latest update of PHP on Enterprise…

11 months ago