1

When I run which trap, it outputs shell built-in command.

$ which trap
trap: shell built-in command

According to the manual:

If arg is not present and -p has been supplied, then the trap commands associated with each sigspec are displayed. If no arguments are supplied or if only -p is given, trap prints the list of commands associated with each signal.

But when I run trap -l and trap -p, it outputs nothing.

On Ubuntu, I get all signals.

 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
31) SIGSYS  34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

How can I output macOS trap signals?

4

Short Answer

Your link appears to be for bash. The latest versions of macOS now use zsh as the default shell. The table below summarizes possible zsh equivalents.

Orignal bash Close zsh Equivalent
trap -l for sig in $(kill -l); do printf "%2s) SIG$sig\n" $(kill -l $sig); done
or
bash -c 'trap -l'
trap -p trap
trap -p sig trap | grep -E sig$
trap -p sig1 sig2 ... trap | grep -E -e sig1$ -e sig2$ ...

Longer More Detailed Answer

Your link appears to be for bash. The latest versions of macOS now use zsh as the default shell.

Under zsh, the command below will list the signal names.

kill -l

For example, the above command produces the following output

HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH INFO USR1 USR2

Also, the command below will list the corresponding signal number for each sig that is a name.

kill -l [ sig ... ]

For example, the command

kill -l INT QUIT ILL TRAP ABRT

will produce the following output.

2
3
4
5
6

You can enter man zshbuiltins to get help with the trap and kill builtins for zsh.


When you entered trap -l, the command -l was associated with no signals. This can be shown to be true by adding a signal to the trap -l command, as shown below.

trap -l USR1

Here, the command -l is associated with the USR1 signal. This can be demonstrated by entering the command given below. This command prints the list of commands associated with USR1 signal

trap | grep -E USR1$

The output from the above command is given below.

trap -- -l USR1

Normally, the command -l would not exist.


The commands below would produce output similar to using the bash builtin trap -l.

for sig in $(kill -l); do printf " '%2s) SIG$sig'" $(kill -l $sig); done | xargs printf "%-15s %-15s %-15s %s\n"

The output from entering the above command is given below.

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGEMT       8) SIGFPE
 9) SIGKILL     10) SIGBUS      11) SIGSEGV     12) SIGSYS
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGURG
17) SIGSTOP     18) SIGTSTP     19) SIGCONT     20) SIGCHLD
21) SIGTTIN     22) SIGTTOU     23) SIGIO       24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGINFO     30) SIGUSR1     31) SIGUSR2

I suppose the above commands could be wrapped in a function, as shown below.

trap-l() { local sig; for sig in $(kill -l); do printf " '%2s) SIG$sig'" $(kill -l $sig); done | xargs printf "%-15s %-15s %-15s %s\n"; }

This would allow the output to be produced by just entering trap-l. However, this might just be folly, since apparently you could have simply entered the command bash -c 'trap -l'.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .