Skip to content
RIPE NCC member · Local Internet RegistryCall us on 020 3318 3449sales@hosting-telecom.uk

Files from Windows: line endings

Why a script from Windows won’t run on the server (“bad interpreter”, ^M) and how to convert the file to Unix format.

Contents

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 ^M at the end of the lines;
  • the command file script.sh reports “with CRLF line terminators”.

How to fix it

The easiest way is with the dos2unix program:

dos2unix script.sh

If 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.sh

On any system, by writing to a new file:

tr -d '\r' < script.sh > script-unix.sh

To 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.

Choose which cookies we may use. You can change your mind at any time with the “Cookie settings” link at the bottom of every page.

Necessary

Always active

They keep the site secure and remember your cookie choice. They don’t identify you.

Google Analytics counts visits and shows which pages people find useful, so we can improve the site. Which cookies