Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if trap is ignored?

Tags:

bash

trap

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
like image 354
KamilCuk Avatar asked Sep 08 '25 15:09

KamilCuk


1 Answers

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) ]]
}
like image 110
oguz ismail Avatar answered Sep 10 '25 12:09

oguz ismail