Skip to content
FR
  1. LuzineDebug
  2. Systems (Windows, macOS, Linux)
  3. /bin/bash^M: bad interpreter: No such file or di…
Systems (Windows, macOS, Linux) Beginner ≈ 5 min to fix

Fix "/bin/bash^M: bad interpreter: No such file or directory"

Error message
/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
In short

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.

Be the first to confirm this fix

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

  1. File created or edited on Windows with an editor set to CRLF (Notepad, VS Code, Notepad++ by default on Windows).
  2. 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.
  3. Script copied into a Docker image from Windows: COPY entrypoint.sh keeps the CRLF, and the container fails with exec /entrypoint.sh: no such file or directory.
  4. 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

  1. Confirm the CRLF line endings

    file reports "with CRLF line terminators", and cat -A (Linux) or cat -e (macOS) shows ^M$ at the end of lines.

    bash
    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
  2. Convert the file to LF

    dos2unix is the simplest (install it with apt, dnf or Homebrew). Otherwise, sed or perl strip the trailing \r. Note: the sed -i syntax differs between Linux and macOS.

    bash
    # 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
  3. Run the script again

    Make sure it is executable, then run it again. chmod +x is enough: do not use chmod 777, which makes the script writable by every user.

    bash
    chmod +x deploy.sh
    ./deploy.sh
  4. Force LF in Git with .gitattributes

    Add a .gitattributes at the repository root so scripts are always checked out with LF, whatever each person's core.autocrlf setting is. Then renormalize the files already tracked and commit. Commit or stash your changes first.

    plaintext
    # .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
  5. 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 .editorconfig file applies the rule to every compatible editor.

    json
    // .vscode/settings.json
    {
      "files.eol": "\n"
    }
  6. 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.

    dockerfile
    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 .gitattributes with *.sh text eol=lf in every project that contains shell scripts.
  • Add an .editorconfig with end_of_line = lf so 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

All Systems (Windows, macOS, Linux) errors →