When I use the trap command in bash, the previous trap for the given signal is replaced.
Is there a way of making more than one trap<
trap 'handler1;handler2;handler3' EXIT
trap "$( trap -p EXIT | cut -f2 -d \' );newHandler" EXIT
handlers="$( trap -p EXIT | cut -f2 -d \' )"
trap "${handlers}${handlers:+;}newHandler" EXIT
trap-add() {
local sig="${2:?Signal required}"
hdls="$( trap -p ${sig} | cut -f2 -d \' )";
trap "${hdls}${hdls:+;}${1:?Handler required}" "${sig}"
}
export -f trap-add
Usage:
trap-add 'echo "Bye bye"' EXIT
trap-add 'echo "See you next time"' EXIT
Remark : This works only has long as the handlers are function names, or simple instructions that did not contains any simple cote (simple cotes conflicts with cut -f2 -d \').