Linux Error: bad interpreter: No such file or directory — Cause, Fix, and Troubleshooting Guide
How to fix the Linux 'bad interpreter: No such file or directory' error: CRLF line endings, wrong shebang paths, missing interpreters, and BOM issues explained.
- #linux
- #troubleshooting
- #shell
- #scripting
Summary
bad interpreter: No such file or directory means the kernel read your script’s shebang (#!) line and could not find the interpreter it points to. The classic and maddening case: the file looks fine, /bin/bash clearly exists, yet it still fails — because a Windows \r (carriage return) is hiding at the end of the shebang line, so the kernel is actually looking for an interpreter named /bin/bash\r. A wrong path, a missing package, or a BOM produces the same message.
Common Symptoms
./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory-bash: ./script: /usr/bin/python: bad interpreter: No such file or directory- A script edited on Windows or pasted through a web form suddenly stops running on Linux.
bash ./script.shworks, but./script.shfails.- The error appears after cloning a repo from a Windows contributor.
Most Likely Causes of the ‘bad interpreter: No such file or directory’ Error
Production causes, most common first:
- CRLF line endings. The file was saved with Windows
\r\n, so the shebang becomes#!/bin/bash\r. The trailing^Mis part of the path the kernel searches for. This is by far the most common cause. - Wrong interpreter path. The shebang says
#!/usr/bin/pythonbut the binary lives at/usr/bin/python3, or#!/bin/bashon a system where bash is at/usr/local/bin/bash. - Interpreter not installed.
#!/usr/bin/pythonon a minimal container that ships no Python at all. - UTF-8 BOM before the shebang. An editor prepended
ef bb bf, so the first bytes are not#!. - Leading whitespace or a blank first line. The
#!must be the very first two bytes of the file. - Distro path differences.
envat/bin/envvs/usr/bin/env; hardcoding the wrong one breaks portability.
Quick Triage
# Reveal hidden carriage returns — look for ^M or \r
cat -A ./script.sh | head -1
# Show the raw first line bytes
head -1 ./script.sh | hexdump -C
# Does the interpreter the shebang names actually exist?
head -1 ./script.sh
A \r shown as ^M at the end of the first line is your smoking gun.
Diagnostic Commands
file ./script.sh
Reports ASCII text, with CRLF line terminators when Windows line endings are present — an instant diagnosis.
head -1 ./script.sh | hexdump -C
Shows the exact bytes of the shebang. Look for a trailing 0d (\r) before the 0a (\n), or a leading ef bb bf BOM before 23 21 (#!).
cat -A ./script.sh | head
cat -A renders \r as ^M and line ends as $. A shebang ending in ^M$ confirms CRLF.
sed -n '1p' ./script.sh | od -c | head
Another way to see the literal characters, including \r and \0.
ls -l /bin/bash /usr/bin/env /usr/bin/python3
Confirm the interpreter path in the shebang actually exists. which bash / command -v python3 tell you where it really lives.
type -a bash python3
Lists every location an interpreter is found, so you can correct the shebang path.
Fix / Remediation
-
Strip CRLF line endings (most common fix). Any of these work:
dos2unix ./script.sh # if installed sed -i 's/\r$//' ./script.sh # portable, no extra packageOn RHEL/Rocky install with
dnf install dos2unix; on Ubuntu/Debianapt install dos2unix. -
Remove a UTF-8 BOM.
sed -i '1s/^\xEF\xBB\xBF//' ./script.sh -
Fix the interpreter path. Point the shebang at the real location, or prefer
envfor portability:#!/usr/bin/env bash #!/usr/bin/env python3envsearchesPATH, sidestepping distro path differences (e.g./binvs/usr/bin). -
Install the missing interpreter.
apt install python3 # Ubuntu/Debian dnf install python3 # RHEL/Rocky -
Ensure
#!is the first two bytes. Delete any blank first line or leading spaces before the shebang.Warning: Before a bulk
sed -iorfind ... -exec dos2unixacross a repo, commit or back up first — a bad glob can rewrite binary files and corrupt them. Scope the find to text extensions (-name '*.sh').
Validation
file ./script.sh # should say: ASCII text (no CRLF)
head -1 ./script.sh | hexdump -C # shebang ends in 0a, not 0d 0a
./script.sh # runs cleanly
Prevention
- Configure your editor to save shell scripts with LF endings (VS Code:
"files.eol": "\n"). - Add a
.gitattributesrule:*.sh text eol=lfso Git normalizes line endings on checkout. - Prefer
#!/usr/bin/env bashover hardcoded paths for cross-distro portability. - Add a CI lint step (e.g.
shellcheckplus a CRLF grep:! grep -rlP '\r$' scripts/) to catch bad endings before merge. - On minimal container images, verify the interpreter is actually present in the final layer.
Related Errors
- Linux Error: No such file or directory (when the file exists)
- Linux Error: Exec format error
- Linux Error: command not found
- Linux Error: Permission denied
Final Notes
When /bin/bash obviously exists but you still get bad interpreter: No such file or directory, stop staring at the path and check the bytes — a hidden \r from a Windows editor is the culprit nine times out of ten. file and cat -A expose it instantly, and sed -i 's/\r$//' fixes it. Standardize on LF endings and env shebangs to keep it from coming back.
Want faster Linux incident response? Use DevOps AI Toolkit to turn production errors into clear diagnostics, remediation steps, and reusable runbooks.
Download the Free 500-Prompt DevOps AI Toolkit
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.