I've tried the following code; I'm not sure if it solves all the problems you've mentioned or not. To use itI can't find this documented anywhere, you call but it seems like adding:
ontrap -return RETURN and pass the cleanup code
as the first argumentlast command in the trap handler, rather than usingcauses the trap to revert to the previous one (trapbash directly.
function on-return-handler () {
if [[ ${RETURN_TRAP_DEPTH[${#RETURN_TRAP_DEPTH[@]}-1]} != ${#FUNCNAME[@]} ]]; then return; fi
eval "${RETURN_TRAP_STACK[${#RETURN_TRAP_STACK[@]}-1]}"
unset RETURN_TRAP_STACK[${#RETURN_TRAP_STACK[@]}-1]
unset RETURN_TRAP_DEPTH[${#RETURN_TRAP_DEPTH[@]}-1]
}
function on-return () {
trap 'on-return-handler' RETURN
RETURN_TRAP_STACK+=( "$1" )
RETURN_TRAP_DEPTH+=( "${#FUNCNAME[@]}" )
}
This uses twois keeping a stack of bashRETURN arrays as stacks, one to store the cleanup code, and one to track the function call depth;handlers somewhere. The call depth is compareddocumentation says that in general trap - {SIGSPEC} causes the return handlertrap to make sure that we only firerevert to the clean updefault; I guess in the actual function that registered it. The values are 'popped' fromthis case, the stack afterdefault is the handler fires'shadowed' trap command.