From 9985f71c922c0d10741a183b179e0ce210b3872c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 19 Sep 2019 12:00:25 +0300 Subject: [PATCH] docs: update --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 3a57943..6d4052c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,9 @@ A collection of pure POSIX `sh` alternatives to external processes * [Miscellaneous](#miscellaneous) * [ARITHMETIC](#arithmetic-1) * [Ternary Tests](#ternary-tests) +* [TRAPS](#traps) + * [Do something on script exit](#do-something-on-script-exit) + * [Ignore terminal interrupt (CTRL+C, SIGINT)](#ignore-terminal-interrupt-ctrlc-sigint) @@ -520,3 +523,26 @@ For use in `[ ]` `if [ ]; then` and `test`. # ': var': If the test fails. var=$((var2 > var ? var2 : var)) ``` + +# TRAPS + +Traps allow a script to execute code on various signals. In [pxltrm](https://github.com/dylanaraps/pxltrm) (*a pixel art editor written in bash*) traps are used to redraw the user interface on window resize. Another use case is cleaning up temporary files on script exit. + +Traps should be added near the start of scripts so any early errors are also caught. + +## Do something on script exit + +```shell +# Clear screen on script exit. +trap 'printf \\e[2J\\e[H\\e[m' EXIT + +# Run a function on script exit. +# 'clean_up' is the name of a function. +trap clean_up EXIT +``` + +## Ignore terminal interrupt (CTRL+C, SIGINT) + +```shell +trap '' INT +```