#Bash 的 trap 命令
trap [OPTION]... [[CMD] SIGNAL_SPEC]
功能
捕获信号或 shell 退出事件,并执行指定命令。
类型
Bash 内置命令。
参数
OPTION选项:-l- 打印信号名称及其对应编号的列表-p- 显示与每个 SIGNAL_SPEC 关联的陷阱命令
CMD- 要执行的命令;如果没有此参数,则清空所有关联的命令SIGNAL_SPEC- 要捕获的信号或:EXIT- shell 退出时触发ERR- 任何命令返回非 0 时触发DEBUG- 每条命令执行前触发RETURN- 函数返回或source加载的脚本结束时触发
#示例
$ TEMP_FILE=$(mktemp) # 创建临时文件
$ trap 'rm -f "$TEMP_FILE"' EXIT # shell 退出时自动删除临时文件
#手册
trap: trap [-lp] [[arg] signal_spec ...]
Trap signals and other events.
Defines and activates handlers to be run when the shell receives signals
or other conditions.
ARG is a command to be read and executed when the shell receives the
signal(s) SIGNAL_SPEC. If ARG is absent (and a single SIGNAL_SPEC
is supplied) or `-', each specified signal is reset to its original
value. If ARG is the null string each SIGNAL_SPEC is ignored by the
shell and by the commands it invokes.
If a SIGNAL_SPEC is EXIT (0) ARG is executed on exit from the shell. If
a SIGNAL_SPEC is DEBUG, ARG is executed before every simple command. If
a SIGNAL_SPEC is RETURN, ARG is executed each time a shell function or a
script run by the . or source builtins finishes executing. A SIGNAL_SPEC
of ERR means to execute ARG each time a command's failure would cause the
shell to exit when the -e option is enabled.
If no arguments are supplied, trap prints the list of commands associated
with each signal.
Options:
-l print a list of signal names and their corresponding numbers
-p display the trap commands associated with each SIGNAL_SPEC
Each SIGNAL_SPEC is either a signal name in <signal.h> or a signal number.
Signal names are case insensitive and the SIG prefix is optional. A
signal may be sent to the shell with "kill -signal $$".
Exit Status:
Returns success unless a SIGSPEC is invalid or an invalid option is given.