9

Is there a way to check what traps have been set (in the current session or script) in Bash?

Ideally, I'd like to be able to get a list of the signals that have a trap assigned to them, but if that's not possible I can just check each signal individually.

Jolta
  • 2,620
  • 1
  • 29
  • 42
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167

2 Answers2

10

Yes.

You can either see all traps, or traps for a specific signal:

$ trap            # show all traps for all signals
$ trap -p SIGINT  # only show traps for SIGINT
$ trap -p EXIT    # only show traps for EXIT
Dale C. Anderson
  • 2,280
  • 1
  • 24
  • 24
7

Let's define a function as follows:

$ function f
> {
> echo trapped
> }

Let's set the trap to that function assigned to SIGINT

$ trap f SIGINT

Let's list set traps:

$ trap
trap -- 'f' SIGINT
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219