Bash Scripting
debt(d3/e3/b5/t7)
Closest to 'default linter catches the common case' (d3), shellcheck catches unquoted vars, missing set -e patterns, and most common bash pitfalls reliably.
Closest to 'simple parameterised fix' (e3), quick_fix is adding set -euo pipefail and quoting variables — small pattern replacements within the script, slightly more than one line across multiple scripts.
Closest to 'persistent productivity tax' (b5), bash automation tends to spread across deploy/CI/ops scripts and slows many workflows as it grows; once entrenched it shapes how operations work.
Closest to 'serious trap' (t7), misconception states bash continues after failed commands by default — contradicts the intuition from most languages where errors propagate, leading to silent partial failures.
Also Known As
TL;DR
Explanation
Bash scripts combine shell commands with variables, conditionals, loops, and functions. Key practices: always start with #!/bin/bash, use set -euo pipefail to fail on errors, quote all variable expansions to handle spaces, use [[ ]] over [ ] for safer conditionals, and prefer $() over backticks for command substitution. ShellCheck is a static analyser that catches common Bash mistakes automatically.
Diagram
flowchart TD
subgraph Script_Structure
SHEBANG[shebang bin bash]
SET[set -euo pipefail<br/>fail on error undefined var pipe fail]
VARS[Variables and functions]
MAIN[Main logic]
SHEBANG --> SET --> VARS --> MAIN
end
subgraph Control_Flow
IF[if condition<br/>then ... fi]
FOR[for item in list<br/>do ... done]
WHILE[while condition<br/>do ... done]
CASE[case var in<br/>pattern action esac]
end
subgraph Best_Practices
QUOTE[Always quote variables<br/>file with spaces]
LOCAL[Use local in functions]
TRAP[trap cleanup EXIT<br/>cleanup on exit]
end
style SET fill:#238636,color:#fff
style TRAP fill:#1f6feb,color:#fff
style QUOTE fill:#d29922,color:#fff
Common Misconception
Why It Matters
Common Mistakes
- Not using set -euo pipefail — scripts continue silently after errors.
- Unquoted variable expansions — $filename breaks if the filename contains spaces; use "$filename".
- Using [ ] instead of [[ ]] — [[ ]] handles empty variables and complex expressions safely.
- Not validating input arguments — scripts crash with cryptic errors when arguments are missing.
Code Examples
#!/bin/bash
# Missing set -e — continues on error
cd /var/www/app
git pull
php artisan migrate # If this fails, next line still runs
php artisan cache:clear
# Partial deployment with failed migration
#!/bin/bash
set -euo pipefail # Exit on error, undefined vars, pipe failures
APP_DIR="/var/www/app"
cd "$APP_DIR"
git pull origin main
echo 'Running migrations...'
/usr/bin/php artisan migrate --force
echo 'Clearing caches...'
/usr/bin/php artisan cache:clear
echo 'Deploy complete'