Fixing Common Bash Command Errors in Ubuntu & WSL: A Practical Guide for Developers
Working with Bash in Ubuntu or WSL (Windows Subsystem for Linux) is powerful—but not always smooth. If you're a developer or DevOps engineer who jumps between systems, you've probably hit confusing command-line errors more than once.
This guide breaks down the most common Bash errors, explains why they happen, and—most importantly—how to fix them. Whether you’re writing scripts, running system tools, or just learning the shell, this article will help you get unstuck quickly.
⚠️ 1. “Command not found”
Error Message:
foo: command not found
Why it happens:
- You mistyped the command name.
- The command isn’t installed.
- The command exists, but the system can’t find it in your
$PATH
.
How to fix it:
- Double-check the spelling.
If it's a custom binary, add the directory to your $PATH
:
export PATH=$PATH:/path/to/your/tool
Install it (if available via apt
):
sudo apt update
sudo apt install foo
Try locating it:
which foo
Bonus Tip: Use tab completion
to avoid typos.
🔐 2. “Permission denied”
Error Message:
bash: ./script.sh: Permission denied
Why it happens:
- The script or file is not executable.
- You're trying to access a file you don't own or have rights to.
How to fix it:
Check file ownership:
ls -l script.sh
Or run it explicitly with Bash:
bash script.sh
Grant execute permission:
chmod +x script.sh
Extra Note: On WSL, be cautious with file permissions in /mnt/c/...
—they might behave differently due to NTFS.
📁 3. “No such file or directory”
Error Message:
bash: ./my_script.sh: No such file or directory
Why it happens:
- The file path is wrong or file doesn’t exist.
- You're on the wrong directory.
- The script has Windows line endings (
\r\n
), which makes Linux think it’s not a real file.
How to fix it:
Convert Windows line endings to Unix format:
dos2unix my_script.sh
# or
sed -i 's/\r$//' my_script.sh
Verify the file:
ls -l my_script.sh
Pro Tip: Always configure your text editor (like VS Code) to use LF line endings on Linux/WSL.
🧾 4. “Bad interpreter: No such file or directory”
Error Message:
bash: ./myscript: /bin/bash^M: bad interpreter
Why it happens:
- The shebang line (
#!/bin/bash
) has a hidden carriage return character (^M
) from Windows.
How to fix it:
Or manually clean it:
sed -i 's/\r$//' myscript
Convert the script to Unix format:
dos2unix myscript
Key Insight: That ^M
at the end of lines is a sure sign of Windows interference.
🧨 5. “Syntax error: unexpected end of file”
Why it happens:
- You're missing
fi
,done
, a closing}
or quote. - An
if
,for
, orwhile
block was never properly closed.
How to fix it:
Use shellcheck
to get line-specific tips:
sudo apt install shellcheck
shellcheck script.sh
Run:
bash -n script.sh
This checks syntax without executing.
🧪 6. “Exec format error” when running a binary
Error Message:
bash: ./binary: cannot execute binary file: Exec format error
Why it happens:
- The file is a Windows binary, or compiled for a different architecture (e.g. ARM vs x86).
How to fix it:
- If it’s Windows-based (
PE32
), you can’t run it in Bash. Find the Linux version or recompile it.
Inspect it:
file binary
🧠 7. “source: not found”
Error Message:
source: not found
Why it happens:
- You're using
source
in a shell that doesn’t support it (like/bin/sh
).
How to fix it:
Or explicitly run it with Bash:
bash -c "source script.sh"
Use the POSIX-compliant version:
. script.sh
🧹 8. Paths with Spaces
Problem:
cd /mnt/c/Program Files/
# Results in: No such file or directory
Why it happens:
- Bash treats the space as a delimiter unless quoted.
How to fix it:
Or escape the space:
cd /mnt/c/Program\ Files/
Quote the path:
cd "/mnt/c/Program Files/"
🪟 9. WSL-Specific: Scripts Opening in Notepad
Why it happens:
- You try to run a
.sh
file from Windows Explorer, not from the WSL terminal.
How to fix it:
- Avoid running Bash files by double-clicking in File Explorer.
Open the Ubuntu/WSL terminal and run:
./your_script.sh
🔧 Additional Tools & Considerations
✅ Set File Executable by Default
Use this only for a dev environment:
git config core.fileMode false
🔄 Convert Line Endings Before Committing
Prevent this problem in Git repos:
# .gitattributes
*.sh text eol=lf
🧠 Keep Learning with These Tools:
shellcheck
: Detect common bugs in shell scripts.set -x
: Debug Bash scripts line-by-line.trap
in scripts: Catch and debug errors.
✅ Finally
Bash is powerful—but picky. These errors often come from small missteps: a missing permission, a stray carriage return, or running a command in the wrong shell. But once you understand what Bash is telling you, fixing it becomes second nature.
If you work in Ubuntu, WSL, or any Linux-based terminal environment, mastering these basics will save you hours of frustration.
Comments ()