From c120ed6e50b2ef1df9804d8ee6267fc1df13e6e8 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Sun, 1 May 2022 11:02:41 +0200 Subject: [PATCH] Added trap notes to the knowledge base --- knowledge base/trap.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 knowledge base/trap.md diff --git a/knowledge base/trap.md b/knowledge base/trap.md new file mode 100644 index 0000000..a0f21e2 --- /dev/null +++ b/knowledge base/trap.md @@ -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