Fix "/bin/bash^M: bad interpreter: No such file or directory"
/bin/bash^M: bad interpreter: No such file or directory
- -bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory
- zsh: ./deploy.sh: bad interpreter: /bin/bash^M: no such file or directory
- /usr/bin/env: 'bash\r': No such file or directory
- ./deploy.sh: line 2: $'\r': command not found
- /usr/bin/env: 'python3\r': No such file or directory
Your script has Windows line endings (CRLF): Linux reads #!/bin/bash followed by an invisible carriage return and looks for a program named /bin/bash\r, which does not exist. Convert the file to LF with dos2unix or sed, then prevent it from coming back with a .gitattributes and the right editor setting.
What this error means
When you run ./script.sh, the kernel reads the first line (the shebang) up to the LF end-of-line character to know which interpreter to run. On Windows, every line ends with CR LF (\r\n). So the CR stays attached to the path: the system looks for /bin/bash\r, and bash displays it as ^M, the notation for that invisible character. The /bin/bash file does exist; the requested name is wrong. The other lines of the script contain \r too, which produces $'\r': command not found if you run it with bash script.sh.
Common causes
- File created or edited on Windows with an editor set to CRLF (Notepad, VS Code, Notepad++ by default on Windows).
- Git with
core.autocrlf=true: on Windows, Git converts files to CRLF on checkout. The script is then used in WSL, a VM or a Docker container mounted on the same folder. - Script copied into a Docker image from Windows:
COPY entrypoint.shkeeps the CRLF, and the container fails withexec /entrypoint.sh: no such file or directory. - Copy-paste or transfer: text pasted from a page or Windows document, FTP or SFTP transfer that keeps the original line endings.
Step-by-step fix
-
Confirm the CRLF line endings
filereports "with CRLF line terminators", andcat -A(Linux) orcat -e(macOS) shows^M$at the end of lines.file deploy.sh # deploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators cat -A deploy.sh | head -n 3 # Linux cat -e deploy.sh | head -n 3 # macOS -
Convert the file to LF
dos2unixis the simplest (install it with apt, dnf or Homebrew). Otherwise,sedorperlstrip the trailing\r. Note: thesed -isyntax differs between Linux and macOS.# dos2unix (sudo apt install dos2unix / brew install dos2unix) dos2unix deploy.sh # Linux (GNU sed) sed -i 's/\r$//' deploy.sh # macOS (BSD sed) sed -i '' $'s/\r$//' deploy.sh # Works on both perl -pi -e 's/\r$//' deploy.sh -
Run the script again
Make sure it is executable, then run it again.
chmod +xis enough: do not usechmod 777, which makes the script writable by every user.chmod +x deploy.sh ./deploy.sh -
Force LF in Git with .gitattributes
Add a
.gitattributesat the repository root so scripts are always checked out with LF, whatever each person'score.autocrlfsetting is. Then renormalize the files already tracked and commit. Commit or stash your changes first.# .gitattributes * text=auto *.sh text eol=lf Dockerfile text eol=lf *.bat text eol=crlf # then, in the terminal: # git add --renormalize . # git commit -m "Normalize line endings" # git ls-files --eol | grep crlf -
Set the editor to LF
In VS Code, click "CRLF" in the bottom-right status bar and pick "LF", or set it in the settings. In Notepad++: Edit > EOL Conversion > Unix (LF). An
.editorconfigfile applies the rule to every compatible editor.// .vscode/settings.json { "files.eol": "\n" } -
Docker: fix at the source, safety net at build time
The real fix is an LF script in the repository (previous steps). In addition, you can strip line endings during the build to guard against a misconfigured machine.
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"]
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Commit a
.gitattributeswith*.sh text eol=lfin every project that contains shell scripts. - Add an
.editorconfigwithend_of_line = lfso every editor on the team saves with LF. - Under WSL, clone your projects into the Linux file system (
~/projects) rather than under/mnt/c.
Frequently asked questions
What does ^M mean?
It is the notation for the carriage return character (CR, \r, ASCII code 13, typed with Ctrl+M). It is invisible in most editors, hence the confusion.
Why does bash script.sh half work while ./script.sh fails?
With bash script.sh, the shebang is ignored, so there is no "bad interpreter". But the \r characters remain on every line and cause errors such as $'\r': command not found. You need to convert the file either way.
Does it affect Python too?
Yes: a CRLF Python script with #!/usr/bin/env python3 gives /usr/bin/env: 'python3\r': No such file or directory. The fix is the same.
Useful tools for this error
More Systems (Windows, macOS, Linux) errors
- Fix "command not found" and "is not recognized as an internal or external command"
- Fix "running scripts is disabled on this system" in PowerShell
- Fix "xcrun: error: invalid active developer path" on macOS
- Fix "SSL certificate problem: unable to get local issuer certificate" (git, curl, pip, npm)
- Fix "user is not in the sudoers file. This incident will be reported."
- Fix "E: Could not get lock /var/lib/dpkg/lock-frontend" (apt, Ubuntu, Debian)
- Fix "App is damaged and can't be opened" on macOS (Gatekeeper)