/bin/bash^M: bad interpreter: No such file or directory means the first line of the script names an interpreter that does not exist, and the ^M gives it away: the file has Windows line endings, so the interpreter’s name is /bin/bash followed by an invisible carriage return, and no such file exists. Without the ^M, the path in the shebang is simply wrong. Both take one command to fix.
What the shebang does#
When you run ./script.sh, the kernel reads the first line. If it starts with #!, whatever follows is the program to run the file with. #!/bin/bash runs bash; #!/usr/bin/env python3 runs whatever python3 is on the PATH. The kernel takes the rest of the line literally, up to the newline. If the line ends with a carriage return, the carriage return is part of the path.
Confirming it#
file script.sh
# script.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
head -1 script.sh | cat -A
# #!/bin/bash^M$ <- the ^M is the carriage return
head -1 script.sh | od -c | head -2
# 0000000 # ! / b i n / b a s h \r \n
Any of the three shows it. The file was created or edited on Windows, downloaded from a site that converted it, or checked out from git with line-ending conversion turned on.
Fixing the file#
sed -i 's/\r$//' script.sh # Linux
sed -i '' 's/\r$//' script.sh # macOS (BSD sed needs the empty backup argument)
dos2unix script.sh # if installed; apt install dos2unix
tr -d '\r' < script.sh > fixed.sh # portable, writes a new file
Or in the editor: VS Code shows the line ending in the status bar, bottom right; click CRLF and choose LF, then save. Vim: :set ff=unix then :w.
Run file script.sh again afterwards; it should no longer mention CRLF.
The other cause: a wrong path#
bash: ./run.sh: /usr/local/bin/bash: bad interpreter: No such file or directory
No ^M, just a path that does not exist on this machine. Bash lives in /bin/bash on Linux and in /bin/bash or /opt/homebrew/bin/bash on macOS depending on how it was installed; a script written for one system fails on another. The portable form uses env to search the PATH:
#!/usr/bin/env bash
The same applies to Python, Node and every other interpreter: #!/usr/bin/env python3 works wherever python3 is installed, and it is what you want inside a virtual environment.
A subtler version: the interpreter exists but the shebang has a space after #! before a relative path, or a typo such as #!/bin/bsh. Read the line character by character.
Scripts with no shebang#
A file with no #! line run as ./script.sh is executed by the current shell, which usually works but not always; and one run by cron or another program may fail with a different message. Always include the shebang.
Stopping it recurring#
Line endings come back every time the file crosses to Windows and back. Fix it at the source:
# .gitattributes in the repository root
*.sh text eol=lf
*.py text eol=lf
Makefile text eol=lf
Git then stores and checks out those files with LF regardless of the platform or the user’s core.autocrlf. Commit the file. In VS Code, "files.eol": "\n" in the workspace settings makes new files LF. Most other editors have the equivalent.
When the scripts run inside WSL or a container but are edited from Windows, the same rules apply; the file system is shared and the line endings travel with the file.
Related errors from the same cause#
syntax error near unexpected token $'\r'or$'{\r'— the script ran, but a later line has a carriage return in a place that breaks the parser.command not foundfor a command that exists — the command name has a carriage return stuck to it.env: python3\r: No such file or directory— the env form of the same shebang problem.
All of them: convert the file to LF.
Questions people ask#
Why does the error show the whole path with ^M rather than just saying line endings?
The kernel does not know what a line ending is; it sees a filename that includes a byte 13 and reports that it cannot find it. The shell prints the byte as ^M.
Can I run the script anyway?
bash script.sh ignores the shebang and runs the file with bash directly. It may still hit carriage-return syntax errors on later lines.
Does this happen with PowerShell scripts?
PowerShell on Windows expects CRLF and is fine with LF. The problem is one-directional: Windows endings on Unix.
What is autocrlf and should I set it?
core.autocrlf tells git to convert line endings on checkout. Per-file rules in .gitattributes are more reliable than a global setting, and they travel with the repository.
Where to go next#
- env: python: No such file or directory — the Python shebang version.
- Bash script not working in crontab — where this error often first appears.
- Error handling in bash scripts — once it runs.