Bash Trap Command
Overview
Sources:
- **
Code Snippet
You can substitute the "echo Booh!"
trap with a function:
then:
Some of the common signal types you can trap:
SIGINT
: user sends an interrupt signal (Ctrl+C
)SIGQUIT
: user sends a quit signal (Ctrl+D
)SIGFPE
: attempted an illegal mathematical operation
You can check out all signal types by entering the following command:
Notice the numbers before each signal name, you can use that number to avoid typing long strings in trap:
one of the common usage of trap is to do cleanup temporary files:
Details
About
This note is about the
trap
command in a Linux Shell, or Bash.
It often comes the situations that you want to catch a special signal/interruption/user input in your script to prevent unpredictable events.
Trap is the command for this use-case: trap <arg/function> <signal>
.
See Also
- Bash Code Snippets
- Linux, Ubuntu, Kali Linux
- Zsh
- Development Map of Content
- Windows Sub-System for Linux (WSL)
Appendix
Note created on 2024-04-15 and last modified on 2024-04-15.
Backlinks
(c) No Clocks, LLC | 2024