IGNORING SIGNALS IN SUB-COMMANDS ====================================================================== If you process some command through `awk`, and want to print some information at the end, like so: some_command | awk ' .... END { print "some info" } ' However this may not work as expected when the user press `ctrl-c`, when we still want awk to print out the processed info so far. This problem can be solved as below: some_command | ( trap '' INT; awk ' .... END { print "some info" }' ) On a high level, this works because `some_command` terminates following SIGINT. On the receiving end of the pipe, awk ignores SIGINT. Since it receives no more data from some_command, the `END` block executes and prints the processed information received so far. Note that ignoring SIGINT in the sub-shell (`trap '' INT`) causes the subprocesses of that shell to ignoring that signal too. However this behavior is not obvious so let me explain. First, POSIX shell standard says: > the shell executes the utility in a separate utility environment (see Shell > Execution Environment) with actions equivalent to calling the execl() function > as defined in the System Interfaces volume of POSIX.1-2017 Then in POSIX `execl()` document, it says: > Except for SIGCHLD, signals set to be ignored (SIG_IGN) by the calling process > image shall be set to be ignored by the new process image. Note that only SIG_IGN is inherited by the subprocesses. And finally, the proper way to ignore a signal in shell, is by trapping with empty string: trap '' INT