Tutorial by Examples

The trap is reset for subshells, so the sleep will still act on the SIGINT signal sent by ^C (usually by quitting), but the parent process (i.e. the shell script) won't. #!/bin/sh # Run a command on signal 2 (SIGINT, which is what ^C sends) sigint() { echo "Killed subshell!" } ...
You can use the trap command to "trap" signals; this is the shell equivalent of the signal() or sigaction() call in C and most other programming languages to catch signals. One of the most common uses of trap is to clean up temporary files on both an expected and unexpected exit. Unfortu...
Have you ever forgotten to add a trap to clean up a temporary file or do other work at exit? Have you ever set one trap which canceled another? This code makes it easy to add things to be done on exit one item at a time, rather than having one large trap statement somewhere in your code, which may...
Trap expressions don't have to be individual functions or programs, they can be more complex expressions as well. By combining jobs -p and kill, we can kill all spawned child processes of the shell on exit: trap 'jobs -p | xargs kill' EXIT
There is a signal WINCH ( WINdowCHange), which is fired when one resizes a terminal window. declare -x rows cols update_size(){ rows=$(tput lines) # get actual lines of term cols=$(tput cols) # get actual columns of term echo DEBUG terminal window has no $rows lines and is $cols chara...

Page 1 of 1