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.