Safe script header

Stop on errors, unset variables and failed pipes, and clean up on exit.

bash beginner #scripting#errors
bash-strict-mode.sh
#!/usr/bin/env bash
set -Eeuo pipefail

tmp="$(mktemp -d)"
cleanup() { rm -rf "$tmp"; }
trap cleanup EXIT
trap 'echo "error on line $LINENO" >&2' ERR

echo "working in $tmp"