Traps and Signals
trap runs a command when the script exits or receives a signal such as INT or TERM. EXIT runs on every exit, which makes it perfect for cleanup.
Example
tmp=$(mktemp)
trap 'rm -f "$tmp"; echo "removed temp"' EXIT
echo data > "$tmp"
wc -c < "$tmp"Result
5 removed temp
Exercise
Run code on exit.
trap 'echo bye' Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.