How to write better scripts
Writing High-Quality Bash Scripts
Always Start with a Strict Header
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'Quote Everything
# Bad
cp $file $destination
# Good
cp "$file" "$destination"Use Functions
Handle Errors Explicitly
Use trap for Cleanup
trap for CleanupValidate Inputs Early
Use [[ ]] Instead of [ ]
[[ ]] Instead of [ ]Prefer $(...) Over Backticks
$(...) Over BackticksName Variables Clearly
Add a Usage/Help Function
Use local in Functions
local in FunctionsLint Your Scripts
Putting It Together — A Template
Last updated