🔧 10 Bash Traps That Are Costing You Hours (And How to Fix Them)
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
A single missing space in a Bash script once cost me three hours of debugging. The worst part? The script looked fine at first glance.
Bash scripting is deceptively simple, but one small mistake can break everything. Worse, some of these issues fail silently, causing data loss, broken automation, or security risks.
This guide covers 10 Bash traps that cost people hours of frustration—and how to fix them instantly.
Need a quick-reference guide for Bash scripting?
👉 Get the Bash Cheat Book for $3.99
1. Not Quoting Variables (Silent Failures & Data Loss)
❌ The Problem
Leaving variables unquoted leads to unexpected word splitting, which can break commands without warning.
🚨 Example of a Bug
filename="my file.txt"
rm $filename # ❌ Tries to delete 'my' and 'file.txt' separately
✅ The Fix
rm "$filename" # ✅ Always quote variables
🔹 Why it matters: A missing quote in a rm
command can delete the wrong files—or worse, everything in a directory.
2. Using [
Instead of [[
in Conditionals
❌ The Problem
Bash has two ways to write conditionals:
-
[ ... ]
(old, POSIX-compliant but error-prone) -
[[ ... ]]
(newer, safer)
Using [ ... ]
can cause unexpected bugs with spaces, regex, and logical operators.
🚨 Example of a Bug
file="myfile.txt"
if [ -f $file ]; then # ❌ Breaks if $file is empty or contains spaces
echo "File exists"
fi
✅ The Fix
if [[ -f "$file" ]]; then # ✅ Safe syntax
echo "File exists"
fi
🔹 Why it matters: [[ ... ]]
prevents syntax errors and unexpected expansions.
3. Ignoring Exit Codes ($?
) and Assuming Success
❌ The Problem
Bash does not stop execution on errors unless explicitly told to.
🚨 Example of a Bug
cp important_file /backup
echo "Backup complete" # ❌ This prints even if `cp` fails
✅ The Fix
Use set -e
to exit immediately on failure:
set -e
cp important_file /backup
echo "Backup complete" # ✅ Runs only if `cp` succeeds
🔹 Alternative Fix: Manually check exit codes:
if cp important_file /backup; then
echo "Backup successful"
else
echo "Backup failed"
fi
🔹 Why it matters: Prevents silent failures that corrupt automation workflows.
4. Hardcoding /bin/bash
Instead of #!/usr/bin/env bash
❌ The Problem
Not all systems have Bash at /bin/bash
, leading to "command not found" errors.
🚨 Example of a Bug
#!/bin/bash
echo "Hello"
Fails if Bash is in /usr/local/bin/bash
.
✅ The Fix
#!/usr/bin/env bash
echo "Hello"
🔹 Why it matters: This finds Bash dynamically, ensuring compatibility across Linux distros.
5. Using cat file | grep
Instead of Just grep
❌ The Problem
Useless use of cat
slows down processing.
🚨 Example of a Bug
cat file.txt | grep "error" # ❌ Unnecessary `cat`
✅ The Fix
grep "error" file.txt # ✅ More efficient
🔹 Why it matters: Speeds up command execution, especially in loops.
6. Running Scripts Without chmod +x
❌ The Problem
If a script lacks execute permissions, running ./script.sh
fails.
✅ The Fix
chmod +x script.sh
Or run explicitly with Bash:
bash script.sh
🔹 Why it matters: Prevents "Permission denied" errors when running scripts.
7. Using find -exec
Instead of xargs
(Performance Issue)
❌ The Problem
Using find -exec
spawns a new process for every file, slowing down execution.
🚨 Example of a Bug
find . -name "*.log" -exec rm {} \; # ❌ Runs `rm` separately for each file
✅ The Fix
find . -name "*.log" | xargs rm # ✅ Much faster
🔹 Alternative: Use -exec ... +
(if supported):
find . -name "*.log" -exec rm {} +
🔹 Why it matters: Massively improves script performance.
8. Overwriting Files Without Warning (>
Instead of >>
)
❌ The Problem
Using >
overwrites files without a prompt.
🚨 Example of a Bug
echo "New data" > important.txt # ❌ Overwrites existing content
✅ The Fix
Use append (>>
) or safe redirection (>|
):
echo "New data" >> important.txt # ✅ Appends instead of overwriting
🔹 Extra Safety: Enable no-clobber mode:
set -o noclobber
🔹 Why it matters: Prevents accidental data loss.
9. Using echo
for Command Output Instead of printf
❌ The Problem
echo
does not always handle special characters and escape sequences correctly.
🚨 Example of a Bug
echo "Column1\tColumn2"
This may not print a tab correctly in some shells.
✅ The Fix
printf "Column1\tColumn2\n"
🔹 Why it matters: printf
ensures consistent output formatting.
10. Using ==
Instead of =
in sh
Scripts
❌ The Problem
Bash supports ==
, but POSIX sh
only supports =
.
🚨 Example of a Bug
#!/bin/sh
if [ "$var" == "hello" ]; then # ❌ Works in Bash, fails in POSIX sh
echo "Match"
fi
✅ The Fix
if [ "$var" = "hello" ]; then # ✅ Compatible with both Bash and sh
echo "Match"
fi
🔹 Why it matters: Ensures portability across different shells.
Final Thoughts: Debug Bash Like a Pro
Fixing these 10 common Bash mistakes will save hours of debugging.
Want a Structured Bash Reference?
If you need a quick reference Bash cheat sheet with real-world scripts, check out my Bash Cheat Book:
👉 Download the Bash Cheat Book for just $3.99
What’s the worst Bash mistake you’ve ever made? Drop a comment below!
...
🔧 Thất nghiệp tuổi 35
📈 38.35 Punkte
🔧 Programmierung
🔧 SILENCE is Costing You More Than You Think!
📈 25.41 Punkte
🔧 Programmierung
🎥 Poor Integrations Are Costing You Your Job
📈 22.35 Punkte
🎥 IT Security Video