What does `trap` do in a shell script?
IntermediateConceptPackages, Shell & Text Tools
Answer
trap runs a command when the shell receives a signal or pseudo-signal (EXIT, ERR).
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
trap 'echo "interrupted"; exit 130' INT TERM
Using EXIT guarantees cleanup of temp files and lock files whether the script succeeds, fails or is interrupted.
More on Packages, Shell & Text Tools
Compare apt and dnf package management.BeginnerWhat is the difference between `apt upgrade` and `apt full-upgrade`?IntermediateHow do you find which package provides a file or command?BeginnerHow do you pin or hold a package version?IntermediateHow are repositories and their GPG keys configured?AdvancedWhat is the difference between a login shell and an interactive non-login shell?Intermediate