How to Fix ‘bash: ./script.sh: /bin/bash^M: bad interpreter’ in Linux

Attempting to execute a shell script in a Linux terminal only to be halted by the error message: ‘bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory’ halts automation pipelines and deployment workflows. Despite /bin/bash existing as a valid system executable on your machine, the Linux kernel refuses to launch the script. This discrepancy occurs because of differing newline encoding standards between operating systems: Windows uses Carriage Return + Line Feed (CRLF or \r\n), whereas Linux and Unix use only Line Feed (LF or \n). When a script authored or edited on Windows is transferred to Linux, the carriage return character is interpreted as part of the shebang binary path, creating an invalid filename: /bin/bash\r.

Why Does the /bin/bash^M Error Occur?

The Linux kernel inspects the first line of an executable script (the shebang line starting with #!) to locate the execution binary. The ^M character represents ASCII carriage return (code 13). Causes include:

  • Editing Scripts in Windows Editors: Creating or modifying scripts in Notepad, VS Code on Windows without setting LF mode, or transferring scripts via copy-paste.
  • Git Checkout Line Ending Conversions: Git configured with core.autocrlf = true, converting LF to CRLF upon cloning onto Windows filesystems or shared mounts.
  • Archive Transfers Across Systems: Unzipping .zip or .tar files created in Windows environments without preserving Unix line endings.

Step 1: Inspect Hidden Carriage Returns with cat or file

Confirm that carriage return bytes are present in your script.

  1. Run the file command against your script:
    file script.sh
  2. If the output displays: Bourne-Again shell script, ASCII text executable, with CRLF line terminators, Windows line endings are confirmed.
  3. Alternatively, view non-printable characters using cat:
    cat -v script.sh | head -n 5
  4. Notice the trailing ^M characters at the end of each line:
    #!/bin/bash^M
    echo "Starting deployment..."^M

Step 2: Convert CRLF to LF Using dos2unix

The standard and most reliable method to sanitize scripts is using the dedicated dos2unix utility.

  1. If not already installed, install dos2unix:
    sudo apt install dos2unix      # Ubuntu / Debian
    sudo dnf install dos2unix      # Fedora / RHEL
  2. Run dos2unix on your script:
    dos2unix script.sh
  3. The utility automatically strips all carriage return characters and converts the file to pure Unix LF format.
  4. Execute your script:
    ./script.sh

Step 3: Strip ^M Characters Using sed or tr

If you cannot install external packages, standard coreutils tools strip carriage returns instantly.

  1. Using sed to remove carriage returns inline:
    sed -i 's/\r$//' script.sh
  2. Alternatively, using tr to output a clean version:
    tr -d '\r' < script.sh > script_fixed.sh
    chmod +x script_fixed.sh
    ./script_fixed.sh

Step 4: Fix Line Endings in Visual Studio Code

If you author scripts in VS Code on Windows for Linux deployment, set the editor’s default line ending.

  1. Open the script in Visual Studio Code.
  2. Look at the bottom status bar in the lower right corner.
  3. Locate the line ending indicator (it will show CRLF).
  4. Click CRLF and select LF from the top command palette dropdown.
  5. Save the file (Ctrl + S).
  6. To enforce LF across all repository files, add a .gitattributes file containing:
    * text=auto eol=lf

Converting CRLF line endings to Unix LF via dos2unix or sed permanently resolves the ‘/bin/bash^M: bad interpreter’ execution error in Linux.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.