Check if trap is set in Bash

Is there a way to check what trapwas installed (in the current session or script) in Bash?

Ideally, I would like to get a list of signals for which there is trapone assigned to them, but if this is not possible, I can simply check each signal individually.

+4
source share
2 answers

Yes .

You can see all the traps or traps for a particular signal:

$ trap            # show all traps for all signals
$ trap -p SIGINT  # only show traps for SIGINT
$ trap -p EXIT    # only show traps for EXIT
+3
source

We define the function as follows:

$ function f
> {
> echo trapped
> }

Set a trap for this function assigned by SIGINT

$ trap f SIGINT

Let the list of given traps:

$ trap
trap -- 'f' SIGINT
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
+5

Source: https://habr.com/ru/post/1654498/


All Articles