The trap Command

Updated

October 31, 2026

The trap Command

trap establishes signal handlers inside shell scripts:

# Register a cleanup function for termination signals and standard script EXIT:
cleanup() {
    echo "Caught signal or exit, cleaning up..."
}
trap cleanup EXIT INT TERM HUP