Why a script from Windows won’t run on the server (“bad interpreter”, ^M) and how to convert the file to Unix format.
In Windows, every line of a text file ends with two characters, CR and LF, while Linux and FreeBSD use just one, LF. A script or settings file saved on Windows may be read incorrectly by the server.
Symptoms
- when you run the script, you see
/bin/bash^M: bad interpreter: No such file or directory; - in vim, you can see
^Mat the end of the lines; - the command
file script.shreports “with CRLF line terminators”.
How to fix it
The easiest way is with the dos2unix program:
dos2unix script.shIf you don’t have it, install it with sudo apt install dos2unix or sudo dnf install dos2unix, or on FreeBSD with sudo pkg install dos2unix. Without installing anything on Linux:
sed -i 's/\r$//' script.shOn any system, by writing to a new file:
tr -d '\r' < script.sh > script-unix.shTo convert back to Windows format, run unix2dos file.txt (from the same package).
How to avoid the problem
Set your editor on your computer to use LF line endings: in VS Code, click “CRLF” at the bottom right and choose “LF”; in Notepad++, go to the “Edit” menu, then “EOL Conversion”, and choose UNIX (LF). If your files reach the server through git, line endings are controlled by the core.autocrlf setting.
Didn’t find the answer? Ask our support team — we’ll help.