Interview Prep/Linux/Packages, Shell & Text Tools

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.

All Linux questions Roadmap: Linux

More on Packages, Shell & Text Tools