Skip to content
Latchkey

Shell script CRLF shebang "No such file or directory" in CI

The script has Windows CRLF line endings, so the shebang line ends in \r. The kernel reads the interpreter as /bin/bash\r, which does not exist, and reports "No such file or directory" even though bash is installed.

What this error means

Running ./script.sh fails with "bad interpreter: No such file or directory", or a \r: command not found error appears on later lines. The file was authored or committed on Windows.

bash
./deploy.sh: line 2: $'\r': command not found
env: bash\r: No such file or directory

Common causes

CRLF line endings from a Windows editor

The file was saved with \r\n. The \r at the end of the shebang is treated as part of the interpreter name, so the lookup fails.

Git re-added CRLF on checkout

A .gitattributes or core.autocrlf setting converted the shell script to CRLF when the runner checked it out.

How to fix it

Convert the file to LF

  1. Strip carriage returns with sed or dos2unix.
  2. Re-run the script to confirm the interpreter resolves.
  3. Commit the LF version.
Terminal
sed -i 's/\r$//' deploy.sh
# or: dos2unix deploy.sh

Force LF for shell scripts in git

Add a .gitattributes rule so shell scripts are always checked out with LF, regardless of the developer OS.

.gitattributes
*.sh text eol=lf

How to prevent it

  • Commit a .gitattributes enforcing eol=lf for *.sh.
  • Configure editors to use LF for shell scripts.
  • Run file script.sh or cat -A to spot ^M before committing.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →