3 ms·
I've used a variation of this that catches other signals as well: trap 'rc=$?; trap "" EXIT; cleanup $rc; exit $rc' INT TERM QUIT HUP trap 'cleanup; exit'
by pedro84 9y ago
I've used a variation of this that catches other signals as well:
trap 'rc=$?; trap "" EXIT; cleanup $rc; exit $rc' INT TERM QUIT HUP
trap 'cleanup; exit' EXIT
- eridius 9y agoThis is unnecessary. The EXIT trap fires on any exit from the shell, not just graceful exit.
- jwilk 9y agoThe behavior varies with shell: $ cat test_trap trap 'echo exiting' EXIT kill $$ $ bash test_trap exiting Terminated $ ksh test_trap exiting Terminated $ mksh test_trap exiting $ dash test_trap Terminated $ zsh test_trap Terminated
- eridius 9y agoThis article is specifically about Bash. I'm not surprised that `trap` behavior varies per shell, and I'd recommend reading the documentation on it.