Added trap notes to the knowledge base

This commit is contained in:
Michele Cereda
2022-05-01 11:02:41 +02:00
parent 4d2234ca0a
commit c120ed6e50

23
knowledge base/trap.md Normal file
View File

@@ -0,0 +1,23 @@
# Trap
## TL;DR
```shell
# Run a command or function on exit, kill or error.
trap "rm -f $tempfile" EXIT SIGTERM ERR
trap function-name EXIT SIGTERM ERR
# Disable CTRL-C
trap "" SIGINT
# Re-enable CTRL-C
trap - SIGINT
```
## Sources
- [Using Bash traps in your scripts]
- [The Bash trap command]
[the bash trap command]: https://www.linuxjournal.com/content/bash-trap-command
[using bash traps in your scripts]: https://opensource.com/article/20/6/bash-trap