39 / 55 · 19 Linux & Command Line · Bash Scripting for QA← prev⊞ allnext →☰ Read as one page
5.9Script Best Practices
| Practice | Why |
|---|---|
Use set -euo pipefail |
Catch errors early instead of silently continuing |
Use local in functions |
Prevent variable leaks between functions |
| Quote all variables | Prevent word splitting: "$VAR" not $VAR |
Use shellcheck |
Static analysis tool that catches common bash mistakes |
| Add usage messages | echo "Usage: $0 <url> [timeout]" |
| Use meaningful exit codes | 0 = success, 1 = failure, 2 = usage error |
| Add comments for non-obvious logic | Future you will thank present you |