2 ms·
Nope, INT is (generally) too specific :) Sometimes of course, you just want to run something on SIGINT and not on normal exit, SIGHUP or whatever. But maybe th
by patrec 6y ago
Nope, INT is (generally) too specific :)
Sometimes of course, you just want to run something on SIGINT and not on normal exit, SIGHUP or whatever. But maybe the main use case for trap is implementing try/finally logic and you really want it to fire whenever the shell exits, not matter how (to clean up resources like temporary files). Bash's `trap EXIT` mostly does what you want ("run this when the shell exits"), but writing something that behaves this way across shells (and doesn't fire multiple times etc). is amazingly painful.
- thomasbacklund 6y agoOk, I though it was to catch CTRL-c :) trap EXIT, does work for me in ash/dash/bash, though, when just letting the code flow exit with and without errors. Not sure your use case, but I can for sure concur that traps are very painful to get right, especially when getting into child/grandchild processes land, taking terminal session, etc in regard.