← 返回命令列表

Linux command

trap 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

List

trap -l

Example

trap

Execute

trap 'echo "Caught signal SIGHUP"' SIGHUP

Remove

trap - SIGHUP SIGINT

Ignore

trap '' SIGINT

说明

trap is a shell builtin that executes commands when the shell receives signals. It's commonly used in scripts for cleanup operations (removing temp files), handling interrupts gracefully, and ignoring signals that would otherwise terminate the script. Common signals include SIGINT (Ctrl+C), SIGTERM (termination request), SIGHUP (hangup), EXIT (script exit), and ERR (command error). The EXIT pseudo-signal executes on normal script exit.

参数

-l
List signal names and numbers
-p _signal_
Print trap commands for specified signals
- _signal_
Reset signal to default behavior
'' _signal_
Ignore the signal

FAQ

What is the trap command used for?

trap is a shell builtin that executes commands when the shell receives signals. It's commonly used in scripts for cleanup operations (removing temp files), handling interrupts gracefully, and ignoring signals that would otherwise terminate the script. Common signals include SIGINT (Ctrl+C), SIGTERM (termination request), SIGHUP (hangup), EXIT (script exit), and ERR (command error). The EXIT pseudo-signal executes on normal script exit.

How do I run a basic trap example?

Run `trap -l` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -l do in trap?

List signal names and numbers