You write a script on Windows, copy it to a Linux server, run it, and get an error that makes little sense:
$ ./deploy.sh
-bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory
Run it as bash deploy.sh instead and you get a different one:
deploy.sh: line 2: $'\r': command not found
There’s nothing wrong with the script. Relatives of these: a Python script fails with /usr/bin/env: 'python3\r': No such file or directory, and a Docker container dies on start with exec /entrypoint.sh: no such file or directory when the file is plainly there. They all have the same cause: the file’s line endings are Windows’ CRLF. Here’s why there are two kinds of line ending, how to tell them apart, and how to fix and avoid the problem.
Why there are two kinds
A “new line” in a text file is really an invisible character, and systems don’t agree on which one:
| System | Line ending | Written | Bytes |
|---|---|---|---|
| Linux, macOS | LF | \n |
0A |
| Windows | CR LF | \r\n |
0D 0A |
It goes back to typewriters. Starting a new line took two moves: pushing the carriage back to the left, the carriage return (CR), and rolling the paper up a line, the line feed (LF). Early teletypes had a character for each, so the end of a line meant sending both CR and LF.
Later systems each made their own choice. Unix decided one character was enough: files store only LF, and the system adds the CR on the way to the terminal. CP/M and then MS-DOS kept the typewriter habit and store CR LF in files, and Windows inherited it from DOS. The early Mac used CR alone until Mac OS X switched to Unix’s LF. So today text files are CRLF on Windows and LF on Linux and macOS.
A Linux terminal still needs both moves to get to the start of the next line, in fact; the kernel turns LF into CR LF as it passes a program’s output to the terminal (that’s the onlcr setting in stty -a). The other way round, the Enter key makes the terminal send CR, and the kernel turns it into LF for the program (icrnl). Files only ever hold the LF.
Why it breaks
On Linux, CR isn’t a line ending, just an ordinary character. Every line of a CRLF file ends with an extra CR, so the script’s first line becomes:
#!/bin/bash\r
The system looks for the interpreter that line names, a program called bash\r, which of course doesn’t exist: bad interpreter: No such file or directory. Docker’s no such file or directory is the same thing: what’s missing isn’t the script but the interpreter with a CR on the end. The ^M in the error is the CR. Control characters are often written as ^ and a letter; CR is code 13, M is the 13th letter, and Ctrl+M in a terminal types it.
$'\r': command not found comes from the empty lines: all that’s left on them is a CR, which bash takes for a command named \r. Other lines go wrong too, just less visibly; cd /opt/app\r, for one, tries to enter a folder whose name ends in a CR.
Python itself reads CRLF just fine, and a .py file with CRLF line endings runs. Only the first line, #!/usr/bin/env python3, causes trouble, because the system and env read that line, and they don’t accept CRLF.
Telling which one a file uses
The file command says it outright:
$ file deploy.sh
deploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
An LF file has no with CRLF line terminators. Or let cat -e show the invisible characters: the $ at the end of each line is the LF, and the ^M before it is the CR:
$ cat -e deploy.sh
#!/bin/bash^M$
^M$
echo "deploying"^M$
Editors show it too: VS Code’s status bar says CRLF or LF at the bottom right, and clicking it switches; Notepad++’s status bar says Windows (CR LF) or Unix (LF); vim shows [dos] at the bottom when it opens a CRLF file.
Fixing it
If the server has dos2unix, it’s one command:
dos2unix deploy.sh
If not, install it (sudo apt install dos2unix, sudo dnf install dos2unix), or skip that and have sed remove the CR at the end of each line:
sed -i 's/\r$//' deploy.sh
That’s GNU sed on Linux; macOS’s sed doesn’t understand \r. tr works everywhere: tr -d '\r' < deploy.sh > deploy-fixed.sh. In vim, :set ff=unix followed by :w saves the file with LF.
The other way round, .bat files for Windows should be CRLF, and unix2dos converts them. When a .bat file is LF, cmd sometimes can’t find the label a goto jumps to.
Stopping it at the source
Better than fixing files again and again is keeping CRLF out. It usually gets in one of these ways:
-
Editor defaults. Editors on Windows mostly default to CRLF for new files. Add
"files.eol": "\n"to VS Code’s settings and new files default to LF. -
Git’s automatic conversion. The Git for Windows installer defaults to “Checkout Windows-style, commit Unix-style line endings”, which is
core.autocrlf=true: the repository stores LF, and checking out on Windows turns it into CRLF. Copy or upload a script straight from that folder and the CRLF goes with it. The surest fix is a.gitattributesfile in the repository saying scripts are LF wherever they’re checked out:*.sh text eol=lfAfter adding it, scripts already checked out only turn LF when checked out again, for example by deleting them and running
git checkout .; if the repository itself holds CRLF, rungit add --renormalize .and commit. Or change only your own computer’s setting withgit config --global core.autocrlf input: CRLF becomes LF when you commit, and nothing is converted on checkout. -
Copy and paste. Text on the Windows clipboard has CRLF line endings, and pasted into an editor that saves exactly what it’s given, the CRs stay in the file.
In ShellRS
ShellRS is a free, open-source SSH client that opens and edits files on the server directly, with no “edit locally, then upload” step:
- Double-click a text file in SFTP, or in Files in the right sidebar, to open it in ShellRS’s editor; the bottom right shows the encoding and line endings, such as “UTF-8 · LF”.
- Saving keeps the file’s own line endings: a CRLF file stays CRLF and an LF file stays LF, and a BOM is kept.
- Pasted text is saved with the file’s line endings: copy a few commands on Windows, paste them into an LF script, and after saving it’s still all LF, with no stray CRs. A file whose line endings were already mixed is saved as it is.
More in the ShellRS manual, under “Editing files” in the SFTP section. ShellRS is available from the download page.