Using trap to react to signals and system eventssuggest change
- trap action sigspec… # Run “action” on a list of signals
- trap sigspec… # Omitting action resets traps for signals
|Parameter|Meaning| |––|––| |-p|List currently installed traps| |-l|List signal names and corresponding numbers
trap utility is a special shell built-in. It’s defined in POSIX, but bash adds some useful extensions as well.
Examples that are POSIX-compatible start with
#!/bin/sh, and examples that start with
#!/bin/bash use a bash extension.
The signals can either be a signal number, a signal name (without the SIG prefix), or the special keyword
Those guaranteed by POSIX are:
Number | Name | Notes —––|———|—–– 0 | EXIT | Always run on shell exit, regardless of exit code 1 | SIGHUP | 2 | SIGINT | This is what
^C sends 3 | SIGQUIT | 6 | SIGABRT | 9 | SIGKILL | 14 | SIGALRM | 15 | SIGTERM | This is what
kill sends by default
Found a mistake? Have a question or improvement idea? Let me know.
Table Of Contents