Blog

Fix Bash bad interpreter errors caused by CRLF line endings

If a Bash script fails on Linux with /bin/bash^M: bad interpreter or /usr/bin/env: ‘bash\r’: No such file or directory, check its line endings. Save the script with LF, then add *.sh text eol=lf to the repo's .gitattributes so future checkouts keep the fix. An AI coding agent editing a script on Windows can leave CRLF on disk even when Git is ready to commit LF.

CRLF puts a carriage return (\r, displayed as ^M) before each newline. In a script's #! line, Linux treats that carriage return as part of the interpreter path or argument. Running bash script.sh skips the interpreter lookup, but Bash still reads the line endings in the body.

Check what Git will commit and what Bash reads

Run these from the repo root. Replace scripts/check.sh with the path that failed.

git status --short
git ls-files --eol -- scripts/check.sh
git check-attr text eol -- scripts/check.sh

i/ shows the line endings in Git's index, the snapshot for the next commit. w/ shows the file on disk. attr/ shows the effective Git line-ending rule. Git ls-files documentation.

Output What to do
i/crlf w/crlf Save the file as LF, add the Git rule, and stage the tracked script again.
i/lf w/crlf Save the local file as LF. If the Git rule is missing, add it so a future Windows checkout also gets LF.
i/lf w/lf Check the exact failing copy in CI or the container. If that copy has LF too, check the interpreter path or executable bit.
w/mixed Save the whole file as LF and check it again.

git ls-files reports tracked or staged files by default. For an untracked script, check its line endings in your editor and run git check-attr before staging it.

Fix the script and add a .gitattributes rule

Open the script in your editor and change its line ending setting to LF. Save it and rerun git ls-files --eol to check w/lf. In the root .gitattributes, add or update this rule.

*.sh text eol=lf

For a shell entrypoint without a .sh extension, add a rule for its path, such as scripts/deploy text eol=lf. Use git check-attr on the actual script to catch an override. If your editor supports EditorConfig, [*.sh] with end_of_line = lf in .editorconfig also helps keep local saves on LF. Git attributes documentation, EditorConfig file format.

Start with the failing file. Staging the whole tree can pull other agent edits into the same commit. Review any edits already staged for the script before running the following commands, which stage its current contents.

git add -- .gitattributes
git add --renormalize -- scripts/check.sh
git ls-files --eol -- scripts/check.sh
git diff --cached --stat
git diff --cached -- .gitattributes scripts/check.sh
git diff --cached --ignore-space-at-eol -- scripts/check.sh

For a new script, use git add -- scripts/check.sh since --renormalize works on tracked files. The last diff helps you spot logic changes in a file where every line appears changed. Read it alongside the full diff because it also hides other whitespace changes at the end of lines. Git add documentation.

Run the same command CI runs

In an Ubuntu test, a CRLF script failed with the bash\r error when run directly. bash scripts/check.sh got past the first line, then failed to parse the if block. Staging with git add --renormalize changed i/crlf to i/lf while w/crlf remained. The same on-disk file still failed. A fresh checkout of the fix with core.autocrlf=true reported i/lf w/lf and the script printed ready.

Check Bash syntax, then use the same invocation as your Linux CI job. For a script CI runs directly, use both commands.

bash -n scripts/check.sh
./scripts/check.sh

If direct execution reports Permission denied, git ls-files --stage -- scripts/check.sh shows the stored file mode. 100755 means executable and 100644 means regular file without the executable bit. If the file is supposed to run directly and the indexed mode is 100644, use git add --chmod=+x -- scripts/check.sh and review the staged diff. Verify the committed result in a fresh Linux checkout. Git add executable-bit option.

If CI runs the script from a container image, run the check inside that image. A bind mount can feed the container the host copy, so the image and the mounted script can have different bytes.

Give your coding agent the failing file and command

Linux CI runs ./scripts/check.sh and reports /usr/bin/env: 'bash\r': No such file or directory.
Show git ls-files --eol and git check-attr for scripts/check.sh.
Preserve the script logic. Convert the local copy to LF and update the repo's shell-script rule if needed.
Keep the diff scoped to those paths. Run the exact CI invocation on Linux and report the result.

If this repo generates shell scripts, put the LF requirement in its AGENTS.md or CLAUDE.md and test a generated file with the Linux command that consumes it.