Bash traps I keep forgetting about

2025-11-19 · 2 min read

Cleaning up temp files no matter how a script exits:

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

set -e does not trigger inside a function called from an if condition, and local x=$(cmd) swallows the exit code of cmd. Declare first, assign second.