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
.zipor.tarfiles 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.
- Run the
filecommand against your script:file script.sh - If the output displays:
Bourne-Again shell script, ASCII text executable, with CRLF line terminators, Windows line endings are confirmed. - Alternatively, view non-printable characters using
cat:cat -v script.sh | head -n 5 - Notice the trailing
^Mcharacters 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.
- If not already installed, install
dos2unix:sudo apt install dos2unix # Ubuntu / Debian sudo dnf install dos2unix # Fedora / RHEL - Run
dos2unixon your script:dos2unix script.sh - The utility automatically strips all carriage return characters and converts the file to pure Unix LF format.
- 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.
- Using
sedto remove carriage returns inline:sed -i 's/\r$//' script.sh - Alternatively, using
trto 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.
- Open the script in Visual Studio Code.
- Look at the bottom status bar in the lower right corner.
- Locate the line ending indicator (it will show CRLF).
- Click CRLF and select LF from the top command palette dropdown.
- Save the file (Ctrl + S).
- To enforce LF across all repository files, add a
.gitattributesfile 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.