trap -p or -l command outputs nothing

Solution 1:

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'.