bashtrap

How to check if trap is ignored?


Subshell traps show parent traps with trap -p, but do not execute them.

Is it possible to get the "do not execute them" state? when trap -p returns the parent state? Do I have to track trap assignments myself?

Consider the following - how to implement is_trap_exit_set function?

is_trap_exit_set() {
  : how to implement this?
}
(
  trap 'echo hello from $BASHPID' EXIT
  is_trap_exit_set  # true
  (
    is_trap_exit_set  # false
    echo $BASHPID exiting
  )
  (
    eval "$(trap -p EXIT)"
    is_trap_exit_set  # true
    echo $BASHPID exiting
  )
  echo $BASHPID exiting
)

The code above outputs:

923059 exiting
923060 exiting
hello from 923060
923058 exiting
hello from 923058

Solution

  • I doubt this is documented, but you can try modifying the trap table before calling trap -p like so:

    is_trap_exit_set() {
      trap - QUIT
      [[ $(trap -p EXIT) ]]
    }