How to Fix ‘/bin/bash^M: bad interpreter: No such file or directory’ in Linux

Cloning a Git repository, transferring an automated deployment script from a Windows workstation, or executing a custom shell script in Linux, WSL (Windows Subsystem for Linux), or a Docker container only to be met with the cryptic terminal error bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory stops builds immediately. This error occurs because of an invisible architectural incompatibility between Microsoft Windows and Unix-like operating systems: how they represent the end of a line of text. Windows text editors terminate lines using two non-printing control characters—Carriage Return followed by Line Feed (CRLF, represented as \r\n). Linux and Unix operating systems strictly expect a single Line Feed (LF, represented as \n). When the Linux kernel parses the shebang line (#!/bin/bash\r), it treats the carriage return ^M as part of the binary filename, searching for a non-existent executable named /bin/bash\r. This guide explains how to identify carriage return pollution and strip Windows CRLF line endings cleanly.

Why Does the Kernel Report ‘/bin/bash^M: bad interpreter’?

When you execute a script in Linux, the kernel system call execve() reads the initial two bytes (#!, or shebang) to determine the program loader:

  • Shebang Binary Lookup: The kernel parses every character following #! up to the newline. In a Windows CRLF file, the carriage return byte (hex 0x0D, visualised as ^M or \r) precedes the newline.
  • Path Resolution Failure: The kernel searches the filesystem for an interpreter located at /bin/bash\r. Because no file with that literal control character exists, the system returns ENOENT (No such file or directory).
  • Git Core Autocrlf Configuration: Windows developers cloning repositories with git config core.autocrlf true automatically convert clean Unix LF files into Windows CRLF on disk, which breaks scripts when mounted into Linux Docker containers or WSL.

Method 1: Convert Line Endings Using dos2unix

The dos2unix utility is the standard, safest Linux tool for converting text files between DOS/Windows and Unix formats.

  1. Install dos2unix on your Linux distribution:
    • On Ubuntu/Debian:
      sudo apt update && sudo apt install -y dos2unix
    • On RHEL/CentOS/Fedora:
      sudo dnf install -y dos2unix
    • On Arch Linux:
      sudo pacman -S dos2unix
  2. Convert the problematic script:
    dos2unix script.sh
  3. The utility replaces all carriage return characters with clean Unix newlines in-place:
    dos2unix: converting file script.sh to Unix format...
  4. Ensure execution permissions are set and run your script:
    chmod +x script.sh
    ./script.sh

Method 2: Strip Carriage Returns Using sed

If dos2unix is not available and you cannot install packages (such as in a minimal production Docker container), use the ubiquitous stream editor sed.

  1. Execute sed to remove the trailing \r characters from every line:
    sed -i 's/\r$//' script.sh
  2. Alternatively, use POSIX character notation:
    sed -i 's/[\r]//g' script.sh
  3. The -i flag applies the changes directly to the file without creating temporary copies.
  4. Run the script immediately to verify successful execution.

Method 3: Remove Carriage Returns Using tr

The standard Unix translate utility tr can strip carriage return bytes quickly.

  1. Use tr to delete all \r characters and write the clean output to a new script:
    tr -d '\r' < script.sh > script_clean.sh
  2. Replace the original script:
    mv script_clean.sh script.sh
    chmod +x script.sh
  3. Verify that the script executes without interpreter errors.

Method 4: Fix Line Endings in Vim or Nano

You can change the file format directly within terminal text editors.

  1. In Vim:
    • Open the script:
      vim script.sh
    • Type :set ff=unix and press Enter. (This sets the fileformat to Unix).
    • Type :wq and press Enter to save and exit.
  2. In VS Code (on Windows):
    • Open the script in Visual Studio Code.
    • Look at the bottom status bar on the right side. You will see CRLF.
    • Click on CRLF and select LF from the command palette dropdown.
    • Save the file (Ctrl + S).

Method 5: Configure Git to Prevent Future CRLF Pollution

Prevent Windows from corrupting Unix shell scripts during Git checkout and commit operations.

  1. In your repository root directory, create or edit a file named .gitattributes:
    nano .gitattributes
  2. Add explicit rules forcing shell scripts to maintain LF line endings:
    *.sh text eol=lf
    *.bash text eol=lf
  3. Configure your local Git client on Windows to avoid modifying line endings automatically:
    git config --global core.autocrlf input
  4. This ensures that shell scripts remain binary-clean across cross-platform developer environments.

Get the best tech tips delivered straight to your inbox.

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