trap - run a command when a signal is received
trap [ACTION] SIGNAL...
Options you will use
EXIT- Run on any exit from the shell - the one to use for cleanup.
ERR- Run when a command fails (with set -e semantics).
INT / TERM- Ctrl+C / polite termination.
-l, -p- List signal names; print the current traps.
Examples
$ tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT INT TERMcleans up on success, failure and Ctrl+C alike
Try trap in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.