/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.

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:
- A shell script with an LF-only contract - shebang lines,
Makefilerecipes,Dockerfileentrypoints,git hooks. Fix the file, then prevent recurrence with.gitattributes. - A repository that checks out CRLF on Windows by default -
core.autocrlf=truewrites CRLF into every text file on checkout. Set*.sh text eol=lfand convert once. - A file that must stay byte-exact - vendored binaries, checksummed artifacts, generated files. Mark them
-textso 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.
# 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 fineReproduce the failure in a container with a two-line script, so the cause is unambiguous:
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 directoryThe same bytes with LF run fine:
printf '#!/bin/sh\necho ok\n' > /tmp/lf.sh && chmod +x /tmp/lf.sh && /tmp/lf.sh
# okKey Comparison Table
| Context | Symptom | Root cause | Fix |
|---|---|---|---|
| Shell script in container | env: 'bash\r': No such file or directory | CRLF in shebang | convert and pin eol=lf |
| Docker entrypoint | exec /entrypoint.sh: no such file or directory | CRLF in the first line | RUN sed -i 's/\r$//' in the image, or fix the repo |
| Makefile recipe | missing separator | CRLF in a rule line | .gitattributes for Makefile |
| Git hook | hook exits 1 with no output | CRLF shebang, hook is exec'd by Git | convert the hook, mark eol=lf |
| SSH key or config | bad permissions or parse error | CRLF in a key file | keep the key -text |
| Python module | SyntaxError on the first line | CRLF is fine for Python, but BOM is not | strip BOM, keep eol as needed |
| Setting | Windows checkout | Linux checkout | Effect on .sh |
|---|---|---|---|
| core.autocrlf=true | CRLF | LF | breaks shell scripts on Windows |
| core.autocrlf=input | LF | LF | safe, normalises on commit |
*.sh text eol=lf | LF | LF | correct regardless of autocrlf |
*.sh -text | unchanged | unchanged | only for byte-exact files |
| no attribute and autocrlf=false | as committed | as committed | depends 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:
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:
* 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 binarytext=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:
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:
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:
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 .githooksCommitting .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:
*.pem -text
*.key -text
*.jar binary
*.sha256 -textIf 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:
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"
doneWhen 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 +xas 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=trueon 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
.gitattributesrule and an editor setting are both needed. - Assuming
fileoutput is authoritative.filereports "with CRLF line terminators" for a file with a single CRLF; confirm the shebang withod -c. - A shebang longer than 127 bytes. The kernel truncates it; keep the interpreter path short and use
envonly wherePATHis predictable. - Windows line endings in a
Makefile. The error becomesmissing 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 documentationThe Linux documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Linux guides
All Linux guidesnginx 502 Bad Gateway: upstream connection refused
The log line names the address and errno, which is the diagnosis. Fix the host, the start-up order, the buffers, or the timeout that actually fired.
Elasticsearch: max virtual memory areas vm.max_map_count is too low
The bootstrap check reads a kernel setting that is not namespaced. Set it on the host, persist it with sysctl.d, and tune nodes instead of containers.
ENOSPC from inotify: the watcher limit, not the disk
Three per-user limits, one misleading error. Find the consumer, set a permanent sysctl drop-in, and know when polling is the honest answer.
apt: Could not get lock /var/lib/dpkg/lock-frontend
The lock names its holder, so read it instead of deleting the file. Wait, time out, or stop unattended-upgrades for the duration.
More in Linux
All Linux guidesThe guides filed next to this one in Linux, in library order.