Prerequisites: Before reading this chapter, you should be familiar with running scripts.
Signals are a limited form of inter–process communication, used to send asynchronous notifications to processes. Within Bash, signals can be sent using the
kill command and are handled by traps. When a process is running in the foreground (that is, we’re waiting for it to finish before being able to run another command) we can also send some signals to the foreground process using keyboard shortcuts.
Let’s start by looking into the above in terms of a signal you might already be familiar with. SIGINT, the keyboard interrupt signal, is sent to the foreground process when pressing Ctrl–c. Try running for example
sleep infinity, which would normally run forever in the foreground. Press Ctrl-c to interrupt it. At this point, Bash prints
^C at the cursor position to indicate where in the output stream of the foreground process the shortcut was pressed, terminates the
sleep process, and returns to the prompt:
^Cis an example of one format for representing a keyboard shortcut: pressing Ctrl (
^) followed by a letter (in this case
C). The letter is always printed in uppercase, whether or not Shift is involved in the keyboard shortcut.
Pressing Ctrl-c on the Bash prompt itself also prints
^C followed by a new prompt, but does not exit the Bash process. This is a handy way to cancel editing the current command without having to erase it before starting the next command.