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 (hex0x0D, visualised as^Mor\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 returnsENOENT (No such file or directory). - Git Core Autocrlf Configuration: Windows developers cloning repositories with
git config core.autocrlf trueautomatically 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.
- Install
dos2unixon 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
- On Ubuntu/Debian:
- Convert the problematic script:
dos2unix script.sh - The utility replaces all carriage return characters with clean Unix newlines in-place:
dos2unix: converting file script.sh to Unix format... - 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.
- Execute
sedto remove the trailing\rcharacters from every line:sed -i 's/\r$//' script.sh - Alternatively, use POSIX character notation:
sed -i 's/[\r]//g' script.sh - The
-iflag applies the changes directly to the file without creating temporary copies. - 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.
- Use
trto delete all\rcharacters and write the clean output to a new script:tr -d '\r' < script.sh > script_clean.sh - Replace the original script:
mv script_clean.sh script.sh chmod +x script.sh - 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.
- In Vim:
- Open the script:
vim script.sh - Type
:set ff=unixand press Enter. (This sets the fileformat to Unix). - Type
:wqand press Enter to save and exit.
- Open the script:
- 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.
- In your repository root directory, create or edit a file named
.gitattributes:nano .gitattributes - Add explicit rules forcing shell scripts to maintain LF line endings:
*.sh text eol=lf *.bash text eol=lf - Configure your local Git client on Windows to avoid modifying line endings automatically:
git config --global core.autocrlf input - This ensures that shell scripts remain binary-clean across cross-platform developer environments.