[Fixed] bash: /bin/bash^M: bad interpreter: No such file or directory: Step-by-Step Troubleshooting Guide

Quick Summary: The error bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory (or /bin/sh^M / /usr/bin/env: 'python3\r': No such file or directory) occurs on Linux and macOS when executing a script that was saved with Windows-style CRLF (\r\n) line endings instead of Unix-style LF (\n) line endings. The kernel reads the invisible carriage return character (^M) as part of the shebang interpreter path, causing execution to fail.

Understanding the Root Causes

When you run an executable script directly (e.g., ./deploy.sh), the Linux or macOS kernel inspects the first line’s shebang (#!/bin/bash) to locate the interpreter binary. Because Unix kernels only treat \n (Line Feed) as a line terminator, any preceding \r (Carriage Return, displayed as ^M) is appended literally to the binary path. Common causes include:

  • Cross-Platform File Editing: Creating or editing shell scripts in Windows editors (Notepad, VS Code with CRLF enabled) and copying them to Linux servers, WSL2, or Docker containers.
  • Git Automatic Line Ending Conversion: Cloning repositories on Windows with core.autocrlf = true, which converts LF to CRLF on checkout before mounting the folder into a Linux Docker container.
  • Legacy File Transfers: Transferring scripts in ASCII mode over FTP or downloading raw text files with Windows line endings.

Step 1: Quick Fix – Strip Carriage Returns with dos2unix or sed

Convert the script’s line endings from CRLF to LF directly in the terminal using dos2unix or built-in Unix utilities like sed:

# Option A: Using dos2unix (recommended if installed)
# Install on Ubuntu/Debian: sudo apt-get install -y dos2unix
# Install on macOS: brew install dos2unix
dos2unix ./script.sh

# Option B: Using GNU sed (Linux) without installing extra tools
sed -i 's/\r$//' ./script.sh

# Option C: Using sed on macOS (BSD sed requires an empty backup extension '')
sed -i '' $'s/\r$//' ./script.sh

# Option D: Using tr to strip all carriage returns into a new file
tr -d '\r' < script.sh > script_clean.sh && mv script_clean.sh script.sh && chmod +x script.sh

Step 2: Fix Line Endings Directly in Vim or VS Code

If you are editing the file interactively on a server or workstation, update the file format setting inside your editor:

# In Vim / Vi:
# 1. Open the file: vim script.sh
# 2. Switch fileformat to unix and save:
:set ff=unix
:wq

# In VS Code:
# Look at the bottom-right status bar, click "CRLF", and select "LF".
# Save the file (Ctrl+S / Cmd+S).

Step 3: Prevent Future CRLF Issues in Git and Docker

If this error repeatedly breaks Docker entrypoints or CI/CD scripts for Windows team members, enforce LF line endings at the repository level using .gitattributes:

# Create or update .gitattributes in your repository root
cat <<EOF >> .gitattributes
# Force LF line endings for all shell scripts and Dockerfiles
*.sh text eol=lf
*.py text eol=lf
Dockerfile text eol=lf
EOF

# Normalize existing files in the Git working tree
git add --renormalize .
git commit -m "Enforce LF line endings for shell scripts"

Verification & Testing Steps

Confirm that all carriage return characters have been removed and that the script executes cleanly:

# 1. Inspect hidden line-ending characters using cat -A or file
file ./script.sh
# Expected healthy output: Bourne-Again shell script, ASCII text executable
# (Broken files will say: "with CRLF line terminators")

# Or inspect the first line explicitly with od/cat
head -n 1 ./script.sh | cat -A
# Expected output: #!/bin/bash$  (NO ^M$ at the end)

# 2. Ensure executable permission is set and run the script
chmod +x ./script.sh
./script.sh

Troubleshooting Summary Comparison

Environment / Tool Primary Root Cause Recommended Solution
Linux Server / Container Script uploaded from Windows with CRLF Run dos2unix script.sh or sed -i 's/\r$//' script.sh
macOS Terminal BSD sed syntax differs from GNU sed Run sed -i '' $'s/\r$//' script.sh or dos2unix
Docker Volume Mounts Git on Windows checked out scripts as CRLF Add *.sh text eol=lf to .gitattributes
Python / Node Shebangs /usr/bin/env: 'python3\r': No such file Convert script to Unix LF and verify with file

Leave a Reply