Fix "/bin/bash^M: bad interpreter" (CRLF line endings)
Bash "bad interpreter" (on Ubuntu 24.04: "required file not found") means CRLF line endings. Strip \r with sed, then pin LF in .gitattributes.
bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directoryThe script has Windows CRLF (\r\n) line endings, so the \r (^M) after #!/bin/bash on the first line is read as part of the interpreter name. There is no file called /bin/bash\r, so the run fails. Strip the trailing \r and it works; pin line endings to LF in the repository so it does not come back.
The message depends on the bash version. Ubuntu 24.04 (bash 5.2.21) does not show the ^M at all, which makes the cause hard to guess:
| Where it ran | Message |
|---|---|
| Rocky 9 · bash 5.1.8 | bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory |
| Ubuntu 24.04 · bash 5.2.21 | bash: line 1: ./deploy.sh: cannot execute: required file not found |
Ubuntu 24.04 · sh (dash) | sh: 1: ./deploy.sh: not found |
Docker ENTRYPOINT | exec /entry.sh: no such file or directory |
If the file clearly exists and you still get "not found", suspect this first.
[1] Check the line endings. file reports with CRLF line terminators:
$ file deploy.shdeploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminatorscat -A shows ^M$ at the end of every line; $ is the line end and the ^M before it is \r:
$ cat -A deploy.sh#!/bin/bash^M$echo "deploy start"^M$cd /tmp^M$echo done^M$[2] Do not work around it with bash deploy.sh. That skips the first line and runs, but the \r on the other lines ends up in arguments and fails in odd ways:
$ bash deploy.shdeploy startdonedeploy.sh: line 3: cd: $'/tmp\r': No such file or directoryThe cd failed, yet the exit code was 0 (the last echo succeeded). The script can go on to run commands in the wrong directory, so fix the file.
[3] Strip the \r. This needs nothing extra installed. Copy the file first if you want to keep the original.
$ cp deploy.sh deploy.sh.bak$ sed -i 's/\r$//' deploy.sh$ file deploy.shdeploy.sh: Bourne-Again shell script, ASCII text executable$ ./deploy.shdeploy startdonedos2unix does the same if you have it (Ubuntu package dos2unix):
$ dos2unix deploy.shdos2unix: converting file deploy.sh to Unix format...[4] Find every affected file at once. -I skips binary files:
$ grep -rlI $'\r' --include='*.sh' /srv/app/srv/app/deploy.sh/srv/app/other.shFix only those with step 3. Windows files such as *.bat and *.ps1 should keep CRLF, so limit the search to shell scripts.
[5] Stop it from coming back. It usually happens when a script is edited or checked out on Windows and then copied to the server. As in the Git documentation's example, pin shell scripts to LF in .gitattributes:
*.sh text eol=lfeol=lf sets the line ending written to the working tree on checkout, and setting eol turns on text as well. Files committed with CRLF before you add this stay as they are; run the normalization the docs describe once (git add --renormalize ., check with git status, then commit).
For scripts baked into a container image, rebuild the image so the fixed file goes in. If an entrypoint fails with exec format error instead, the shebang line is missing entirely; see Docker exec format error.