Kyle Strand
Kyle Strand

Reputation: 16529

Check if trap is set in Bash

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.

Upvotes: 9

Views: 4254

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140307

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

Upvotes: 7

Dale C. Anderson
Dale C. Anderson

Reputation: 2460

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

Upvotes: 10

Related Questions