You upload a shell script you wrote on your laptop, make it executable, run it, and the server answers with something that looks like gibberish: /bin/bash^M: bad interpreter: No such file or directory. The file is right there. Bash is definitely installed. So what's going on? Nine times out of ten it's invisible Windows line endings, and it takes about thirty seconds to fix once you know what to look for.
Symptom: what you'll actually see
The error shows up in a few different shapes depending on how the script is run:
-bash: ./backup.sh: /bin/bash^M: bad interpreter: No such file or directorywhen you run it directly over SSH/usr/bin/env: 'bash\r': No such file or directoryor'python3\r'when the shebang usesenv$'\r': command not foundorsyntax error near unexpected token $'do\r'when you run it asbash script.sh- A cron job in wordpress-missing-a-temporary-folder-upload-error" class="auto-link">cPanel that "runs" but never does anything, with the same errors buried in the cron email or log
- Variables with a mystery trailing character, so paths like
/home/user/backups\rdon't exist andcdfails
If the script works fine on your own machine and only breaks on the server, you're almost certainly in the right article.
Cause: CRLF vs LF line endings
Windows ends every line of a text file with two characters: a carriage return and a line feed (\r\n, often called CRLF). Linux expects just the line feed (\n, LF). When Linux reads your first line, #!/bin/bash, it doesn't see /bin/bash. It sees /bin/bash\r, and there's no program with that name. The ^M in the error is just how the terminal displays that stray \r.
This sneaks in more often than people expect:
- Editing the script in Notepad or an editor set to Windows line endings
- Git on Windows with
core.autocrlf=true, which converts files to CRLF on checkout - Copy-pasting a script from a web page or chat app into a Windows editor
- Uploading through some FTP clients in "binary" mode after the file was already converted
Fix: confirm it first
Don't guess. Check the file on the server:
file backup.sh
If you see with CRLF line terminators in the output, that's your culprit. For a more visual check:
cat -A backup.sh | head -5
Lines ending in ^M$ have Windows endings. Clean lines end in just $. You can also count the affected lines:
grep -c $'\r' backup.sh
Fix: convert the file to LF
Pick whichever tool is available. They all do the same job.
Option 1: dos2unix (cleanest)
# Ubuntu / Debian
sudo apt install dos2unix
# AlmaLinux / Rocky
sudo dnf install dos2unix
dos2unix backup.sh
It converts in place and leaves a clean file. On shared cPanel hosting you usually can't install packages, so use option 2.
Option 2: sed (works everywhere)
sed -i 's/\r$//' backup.sh
This strips the carriage return from the end of every line. It's available on every Linux server, including jailed cPanel shells. Want a safety copy first? Use sed -i.bak 's/\r$//' backup.sh and you'll get backup.sh.bak alongside.
Option 3: fix a whole folder at once
If you uploaded a project with dozens of scripts, convert them in one go:
find /home/youruser/scripts -type f -name "*.sh" -exec sed -i 's/\r$//' {} +
Be careful pointing this at directories with binary files (images, archives, compiled files). Stick to a specific extension like *.sh, *.py or *.pl.
Then re-run it
chmod +x backup.sh
./backup.sh
If file backup.sh now just says Bourne-Again shell script, ASCII text executable with no CRLF mention, you're done.
When it's a cPanel cron job
Cron makes this harder to spot because you don't see the error live. A few things to check in cPanel → Cron Jobs:
- Make sure the Cron Email field has an address you actually read. The error text lands there.
- Or redirect output to a log so you can inspect it:
/bin/bash /home/youruser/scripts/backup.sh >> /home/youruser/cron.log 2>&1 - Check the log after the next run. If you see
$'\r': command not found, convert the script with thesedcommand above using cPanel's Terminal.
No Terminal access? Open the file in File Manager → Edit. cPanel's editor saves with Unix line endings, so opening the file, making any tiny change (add and remove a space), and saving will often clean it. Verify with the cron log afterward rather than assuming.
It's not only shell scripts
CRLF breaks other things on Linux servers too, and the errors look unrelated:
| File type | Typical error | Fix |
|---|---|---|
| Python script | /usr/bin/env: 'python3\r': No such file or directory | sed -i 's/\r$//' app.py |
| Perl / CGI script | 500 Internal Server Error, "Premature end of script headers" | Convert, then chmod 755 |
.env file | DB password "wrong" even though it's correct | Convert; the \r becomes part of the value |
| Crontab file (VPS) | Jobs silently skipped | Convert, then reload with crontab file.txt |
| Nginx / systemd config | Odd "unknown directive" or path not found | Convert, then nginx -t or systemctl daemon-reload |
The .env case catches a lot of Laravel users. The app reads DB_PASSWORD=secret\r, MySQL rejects it with an access-denied error, and everyone spends an hour resetting passwords that were never the problem.
Other "bad interpreter" causes worth ruling out
If file doesn't show CRLF and you still get the error, check these:
- Wrong path in the shebang. Run
which bashorwhich python3and compare it with your first line. Scripts written for macOS sometimes point to/usr/local/bin/bash, which doesn't exist on most servers.#!/usr/bin/env bashis the portable choice. - A UTF-8 BOM at the start. Some Windows editors add three invisible bytes before
#!. Check withhead -c 3 script.sh | xxd. If you seeefbb bf, remove it withsed -i '1s/^\xEF\xBB\xBF//' script.sh. - A filesystem mounted
noexec. On some VPS setups/tmpis mounted noexec. You'll get "Permission denied" rather than "bad interpreter", but it's easy to confuse. Run it from your home directory or call it asbash script.sh.
Prevention: stop it coming back
Fixing one file is easy. Not having to do it every deploy is better.
- Set your editor to LF. In VS Code, click
CRLFin the bottom status bar and switch it toLF, or set"files.eol": "\n"in settings. Notepad++ has it under Edit → EOL Conversion → Unix (LF). - Add a
.gitattributesfile to your repository so Git always checks out scripts with LF, no matter who clones it:* text=auto *.sh text eol=lf *.py text eol=lf .env text eol=lf - On Windows, set Git to leave endings alone on commit:
git config --global core.autocrlf input. - Add a check to your deploy step. Something like
grep -rl $'\r' --include="*.sh" . && echo "CRLF found" && exit 1in a GitHub Actions job will catch it before it ever reaches the server.
Once your editor and Git are set up properly, this error pretty much disappears for good.
Frequently Asked Questions
What does ^M mean in a Linux error message?
It's the terminal's way of showing a carriage return character (\r). Its presence in an error almost always means the file was saved with Windows line endings and needs converting to Unix (LF) endings.
Is it safe to run sed -i 's/\r$//' on my script?
Yes, for text files. It only removes a carriage return at the end of each line and doesn't touch anything else. Use sed -i.bak if you'd like a backup, and avoid running it on binary files like images or archives.
Why does the script work when I run "bash script.sh" but fail with "./script.sh"?
Running ./script.sh makes the kernel read the shebang line, which is where the \r breaks the interpreter path. Running bash script.sh skips the shebang, but the script will usually fail a few lines later with $'\r': command not found instead. Converting the file fixes both.
Can I fix this without SSH access on shared hosting?
Often, yes. Open the file in cPanel's File Manager editor and save it, which writes Unix line endings. Better still, fix your local editor's line-ending setting and re-upload, so the problem doesn't follow you to the next file.
Will changing line endings break the script on Windows?
Not for shell scripts, which only run on Linux or macOS anyway. Modern Windows editors, including Notepad, read LF files fine. If you also maintain Windows batch files (.bat), keep those as CRLF using a *.bat text eol=crlf rule in .gitattributes.
