trap Command: Run Code On Signals
trap runs code on signals. Cleanup. Error handling. Useful.
Here's the thing: trap runs code on exit. Use it for cleanup.
Basic trap
trap 'echo "Exiting"' EXIT
My take: trap runs code on exit. Use it.
Cleanup
temp_file="/tmp/temp_$$"
trap "rm -f $temp_file" EXIT
My take: trap cleans up. Use it.
Error Handling
trap 'echo "Error on line $LINENO"' ERR
My take: trap handles errors. Use it.
Common Patterns
Cleanup on Exit
temp_file="/tmp/temp_$$"
trap "rm -f $temp_file" EXIT INT TERM
What's Next?
Now that you understand trap, let's talk about Error Logging.
Personal note: trap seemed complex at first. Then I used it for cleanup. Now I use it always. It's useful. Learn it.