Fixing ‘bash: : command not found’ with Dos2Unix

Written by

in

Fixing ‘bash: : command not found’ with Dos2Unix You run a Bash script, and it fails immediately. The terminal prints a cryptic error: bash: : command not found (often with a strange blank space or a hidden carriage return character
).

This frustrating error usually happens when a script written on Windows is executed on a Linux or macOS system. Windows and Unix-like systems handle the hidden characters at the end of text lines differently.

Here is exactly why this happens and how to fix it instantly using the dos2unix utility. The Cause: Line Ending Mismatch

Windows and Linux use different invisible characters to signify the end of a line of text:

Windows (CRLF): Uses a Carriage Return (
) followed by a Line Feed (
). Linux / macOS (LF): Uses only a Line Feed (
).

When you run a Windows-created script in Linux, Bash reads the
character as part of your command. Instead of executing clear, it tries to execute clear . Since clear does not exist, Bash throws the command not found error. The Solution: Using Dos2Unix

The absolute fastest way to resolve this issue is with dos2unix. This lightweight command-line tool automatically strips out Windows CRLF line endings and converts them to Linux-compatible LF line endings. 1. Install Dos2Unix

If you do not have the utility installed, grab it using your system’s package manager: Ubuntu / Debian / Mint: sudo apt install dos2unix Use code with caution. Red Hat / CentOS / Fedora: sudo dnf install dos2unix Use code with caution. macOS (via Homebrew): brew install dos2unix Use code with caution. 2. Convert Your Script

Once installed, run the command followed by the path to your broken script: dos2unix my_script.sh Use code with caution.

The tool will convert the file in place. You can now run your script normally: chmod +x my_script.sh ./my_script.sh Use code with caution. Alternative Quick Fixes

If you cannot install dos2unix right now, you can use these built-in Linux tools to achieve the same result.

The stream editor (sed) can strip out the trailing
characters: sed -i ’s/ $//’ my_script.sh Use code with caution.

The translate (tr) command can delete the carriage returns, though it requires creating a new file: tr -d ‘ ’ < my_script.sh > clean_script.sh Use code with caution. How to Prevent It in the Future

To stop this error from happening again, configure your text editor to use Unix line endings by default:

VS Code: Look at the bottom right status bar. If it says CRLF, click it and change it to LF.

Git Configuration: Run git config –global autocrlf input to ensure Git automatically converts files to LF when you clone or commit them on Linux/macOS systems. To help narrow down your setup, let me know: What text editor or IDE do you use to write your scripts?

Are you sharing these files across a local network or via Git?

I can provide specific settings to automate this fix for your workflow.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *