What does `trap` do and give a common use.
⚡
Quick Answer
trap runs a handler on signals or pseudo-signals like EXIT, ERR, INT — commonly for cleanup.
Detailed Answer
trap 'cleanup' EXIT guarantees cleanup runs however the script ends. trap on ERR can log context on failure; on INT/TERM you can gracefully stop. It's how robust scripts avoid leaving temp files, locks, or partial state behind.
💡
Interview Tip
EXIT-trap cleanup is the canonical example.
bashtrapsignals