What is a good way to override bash
trap handlers that don't permanently trample existing ones that may or may not already be set? What about dynamically managing arbitrary chains of trap routines?
Is there a way to save the current state of the trap handlers so they can be restored later?
Save and Restore Your Trap Handler State in Bash
I would submit the following stack implementation to track and restore trap state. Using this method, I am able to push trap changes and then pop them away when I'm done with them. This could also be used to chain many trap routines together.
See the following source file (.trap_stack.sh)
This can manage handlers that are defined as named functions and also ad-hoc routines defined like this example
trap "kill $!" SIGTERM SIGINT
.This is the test script I used to help me write it: