Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

/usr/bin/env: bash with a carriage return: no such file or directory

A CRLF shebang names an interpreter that cannot exist. Normalise the file and stop the next one with .gitattributes.

~5 min readLinux
#Linux#Shell#Docker#Git
A script sheet with a hidden carriage symbol and an amber interpreter socket that does not fit

Introduction & Quick Summary

A script that works in the repository fails the moment it is mounted into a container or run on a Linux server:

/usr/bin/env: 'bash\r': No such file or directory

or, in Docker, exec /entrypoint.sh: no such file or directory for a file that plainly exists. The \r in the first message is the whole diagnosis: the shebang is #!/bin/bash\r, and Linux is looking for an interpreter literally named bash followed by a carriage return.

The file was saved with Windows (CRLF) line endings and executed without conversion. Three distinct situations produce it, and they need different fixes:

  1. A shell script with an LF-only contract - shebang lines, Makefile recipes, Dockerfile entrypoints, git hooks. Fix the file, then prevent recurrence with .gitattributes.
  2. A repository that checks out CRLF on Windows by default - core.autocrlf=true writes CRLF into every text file on checkout. Set *.sh text eol=lf and convert once.
  3. A file that must stay byte-exact - vendored binaries, checksummed artifacts, generated files. Mark them -text so no filter touches them.

dos2unix fixes one file; .gitattributes fixes the class. Both are one line of work, and the second saves the next person from finding this page.

Core Mechanics, Signatures & Reproducible Setup

The kernel reads the shebang as a literal path. #!/bin/bash\r is parsed as the interpreter /bin/bash with the argument \r, or, with env, as the program name bash\r. env then searches PATH for an executable whose name ends with a carriage return, does not find one, and prints the message above. Nothing about file permissions or Docker is involved, which is why chmod +x never helps.

bash
# see it without an editor head -c 32 script.sh | od -c | head -2 # 0000000 # ! / u s r / b i n / e n v b # 0000020 a s h \r \n file script.sh # script.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators # confirm the interpreter lookup command -v 'bash' || echo "no bash with a carriage return" # expected bash --version # works, so bash itself is fine

Reproduce the failure in a container with a two-line script, so the cause is unambiguous:

bash
printf '#!/bin/sh\r\necho ok\n' > /tmp/crlf.sh && chmod +x /tmp/crlf.sh && /tmp/crlf.sh # /usr/bin/env: 'sh\r': No such file or directory

The same bytes with LF run fine:

bash
printf '#!/bin/sh\necho ok\n' > /tmp/lf.sh && chmod +x /tmp/lf.sh && /tmp/lf.sh # ok

Key Comparison Table

ContextSymptomRoot causeFix
Shell script in containerenv: 'bash\r': No such file or directoryCRLF in shebangconvert and pin eol=lf
Docker entrypointexec /entrypoint.sh: no such file or directoryCRLF in the first lineRUN sed -i 's/\r$//' in the image, or fix the repo
Makefile recipemissing separatorCRLF in a rule line.gitattributes for Makefile
Git hookhook exits 1 with no outputCRLF shebang, hook is exec'd by Gitconvert the hook, mark eol=lf
SSH key or configbad permissions or parse errorCRLF in a key filekeep the key -text
Python moduleSyntaxError on the first lineCRLF is fine for Python, but BOM is notstrip BOM, keep eol as needed
SettingWindows checkoutLinux checkoutEffect on .sh
core.autocrlf=trueCRLFLFbreaks shell scripts on Windows
core.autocrlf=inputLFLFsafe, normalises on commit
*.sh text eol=lfLFLFcorrect regardless of autocrlf
*.sh -textunchangedunchangedonly for byte-exact files
no attribute and autocrlf=falseas committedas committeddepends on the commit

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Convert the file and commit the conversion

One pass over the repository, with the intent recorded in the commit message so a review does not mistake it for a logic change:

bash
git ls-files -z '*.sh' '*.bash' 'bin/*' | xargs -0 file | grep CRLF git ls-files -z '*.sh' 'bin/*' | xargs -0 sed -i 's/\r$//' git diff --stat git commit -am "chore: normalise shell scripts to LF"

sed -i 's/\r$//' is available everywhere; dos2unix is a nicer tool where it exists. Both strip a trailing carriage return and leave the rest of the bytes alone.

2. Stop it at the source with .gitattributes

This is the durable fix. Normalise text on commit, force the endings that must be exact, and mark binaries so they are never converted:

gitattributes
* text=auto eol=lf *.sh text eol=lf Makefile text eol=lf *.bat text eol=crlf *.cmd text eol=crlf *.ps1 text eol=crlf *.png binary *.webp binary *.woff2 binary *.tar.gz binary

text=auto with eol=lf is the setting that makes a repository behave the same on macOS, Linux, Windows and in CI containers. After adding it, renormalise once:

bash
git add --renormalize . git status --short | head git commit -m "chore: enforce LF via gitattributes"

3. Docker: the copy happens before any conversion

An image built from a Windows checkout inherits the CRLF bytes, and RUN cannot fix the entrypoint after it has been chosen. Convert inside the image when you cannot fix the repository:

dockerfile
COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN sed -i 's/\r$//' /usr/local/bin/entrypoint.sh \ && chmod +x /usr/local/bin/entrypoint.sh ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

Prefer ENTRYPOINT ["/bin/sh", "-c", "..."] only when you truly need a shell; the exec form with a converted script gives better signal handling.

4. Git hooks are files too

A pre-commit hook installed from a Windows IDE can carry CRLF, and Git executes it as a program, so the failure looks like "the hook does nothing". Check the hook itself rather than the hook's logic:

bash
file .git/hooks/pre-commit sed -n '1p' .git/hooks/pre-commit | od -c | head -1 # fix, then make hooks reproducible for the team git config core.hooksPath .githooks

Committing .githooks/ with eol=lf makes the hook identical on every machine and removes the per-clone install step from the onboarding notes.

5. Files that must not be touched

A vendored certificate, a .pem, a .jar, a checksum file or a generated protobuf must be byte-identical on every platform. text=auto will still treat some of them as text and rewrite the endings. Mark them explicitly:

gitattributes
*.pem -text *.key -text *.jar binary *.sha256 -text

If a checksum file is normalised by a filter, the hash it contains no longer matches the artifact, which produces a failure far from the cause.

6. The BOM variant of the same mistake

Windows editors can also write a UTF-8 BOM, which breaks shebangs differently (#!/bin/bash with three bytes in front) and breaks shell variable names and some parsers. Detect and strip both problems at once:

bash
grep -rIl $'\xEF\xBB\xBF' . --exclude-dir=node_modules | head for f in $(grep -rIl $'\xEF\xBB\xBF' . --exclude-dir=node_modules); do sed -i '1s/^\xEF\xBB\xBF//' "$f" done

When comparing two versions of a script to see what a normalisation actually changed, load them side by side in Text Diff: the visible <CR> markers make a whitespace-only diff obvious, so a review does not stall on a 400-line "change" that is one character wide.

Common Compilation/Runtime Mistakes & Version Gotchas

  • chmod +x as the first reaction. Permissions were never the issue; the interpreter name contains a carriage return.
  • Converting in the Dockerfile only. Every local run, CI script and hook keeps failing, and the repository keeps the wrong bytes.
  • core.autocrlf=true on Windows without attributes. It converts on checkout and on commit, so shell scripts break locally while working in CI, which makes the cause look environment-specific.
  • Editing the file in an editor that saves CRLF. The fix is lost on the next save; the .gitattributes rule and an editor setting are both needed.
  • Assuming file output is authoritative. file reports "with CRLF line terminators" for a file with a single CRLF; confirm the shebang with od -c.
  • A shebang longer than 127 bytes. The kernel truncates it; keep the interpreter path short and use env only where PATH is predictable.
  • Windows line endings in a Makefile. The error becomes missing separator, which points at tabs and hides the real cause.

Primary references: gitattributes documents text, eol and -text with their precedence over core.autocrlf, and env(1) describes the interpreter lookup that produces the bash\r message.

Official references

primary documentation

The Linux documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Linux guides

More in Linux

All Linux guides

The guides filed next to this one in Linux, in library order.