Skip to content

env: bash\r: No such file or directory – Windows line endings

Linuxtested on Bash 5.3, Ubuntu 26.04

After editing on another machine the script no longer starts. env looks for a program called bash with a trailing carriage return – because every line ends in CRLF.

Symptom

notes-backup; echo "Exit: $?"
env: $'bash\r': No such file or directory
env: use -[v]S to pass options in shebang lines
Exit: 127

Exit 127 means: program not found – it looked for bash with a carriage return.

Diagnosis

Make the line endings visible.

file ~/sh-lab/werkzeug/bin/notes-backup | grep -o 'with CRLF.*'
head -1 ~/sh-lab/werkzeug/bin/notes-backup | cat -A
with CRLF line terminators
#!/usr/bin/env bash^M$

Causes

^MCRLF instead of LF

An editor or Git on Windows saved the file with carriage return plus line feed. cat -A shows the \r as ^M.

Fix

Remove the carriage returns and compare with the state in Git.
sed -i 's/\r$//' ~/sh-lab/werkzeug/bin/notes-backup
git -C ~/sh-lab/werkzeug status --short | wc -l
0

Don't: Change the shebang to /bin/bash and hope. The \r is still in every other line.

▶
Break it yourself and find it – free
Shell scripting hands-on, Day 15: competition – repair five broken tools against the clock.
Go to day 15 →

FAQ

How do I prevent this for good?

With .gitattributes and *.sh text eol=lf, plus an editor that writes LF.

Why “No such file” when the file exists?

It refers to the interpreter in the shebang: bash with a trailing \r does not exist.

Related error patterns

More