It has not been long since big-brained humans had invented the Graphical User interfaces (GUI). Just before it had emerged, everyone had to use a terminal only for all of their computing tasks. At least, it was all people knew about computers in the early days. To carry out specific jobs in a traditional TUI (Terminal User Interface) program, similar to what you would do on a GUI program, there were keyboard shortcuts set to different commands that the terminal understood as signals to be assigned to a process.
For instance, if you want to minimize a program that has a GUI, along with titlebar buttons, you would be able to press the Minimize button with your mouse to make it happen. On the other hand, you’ll need to press a keyboard shortcut in a terminal if you want to achieve something similar, if not exactly minimizing the program. It’ll instead be detaching a process on screen to run a separate program – which is off to a later topic.
Behold, young wizards! The terminal days aren’t over yet. UNIX Systems still exist, and quite widely so. Linux is one of them. And there are Linux Wizards who know perfect control of their terminal even in this era of Graphical Things on a screen. You don’t need to know every single command out there available to you for use inside a Linux Terminal, but there’s one that you MUST KNOW. It’s Ctrl + C, which I like to refer to as The Terminator (hehe).
Signals in Linux Terminals – what are they?
Signals are a way for the terminal to communicate with the running processes in a system. Even though they are quite limited in terms of what events they can trigger, they form a big part of both old and modern computing. They are used to quit, pause and or kill terminal programs while they are being run. Signals can be sent by the processes themselves, by using commands like kill(), in the program. Otherwise, it is also possible to employ signals by using keyboard shortcuts, by the user.
The SIGINT Signal for Ctrl + C
SIGINT is one of the many signals available in Linux. It is short for “Signal: Interrupt”. What happens is that when you press Ctrl + C inside a Linux Terminal when a process is currently being run, the shortcut will send the SIGINT signal to the terminal. This will tell the terminal to interrupt the process being run on the foreground, and then quit or terminate it. Here’s a little demo:
Use cases for Ctrl + C
Ctrl + C is probably the most commonly used keyboard shortcuts inside a Terminal Shell, be it Linux or be it any other UNIX system. In fact it is so common that people actually end up using it without even knowing what it is. Sometimes, it’s a disaster. Because modern GUI programs have the shortcut to copy strings of text by using Ctrl + C. This causes a lot of terminal-beginners to expect that it’ll work the same in a terminal as well, and try to copy something from the terminal. Now if they try this while a process or a terminal program is being run, it’ll quit out of it, unless the program has the code to bypass the signal, like the text editor called Vim. Nevertheless, here are a few use cases where you might find Ctrl + C inside a terminal to be useful, as long as you don’t expect to paste stuff.
Exiting a frozen process
The most common of all is terminating a process in the foreground, which is frozen, and is no longer proceeding, while not allowing the end user to interact with the terminal. In such cases, you can press Ctrl + C to interrupt that frozen process and terminate it.
Besides, there are times when the program is not actually frozen, but is incapable of proceeding due to lack of data management, or whatever reason it might be. One other common time when I find myself using Ctrl + C is while trying to install a program in my system using a package manager. The Internet connection here goes off a lot, and if it is while a program is being downloaded, it stops and is not able to finish it. You probably know what I do there to fix it, don’t you?
Coming out of unnecessary loops
Yes. This is yet another recurrent use case for Ctrl + C. Terminals can get quite conky quite often, especially when a loop is not broken out of, and it’s not rare at all. Take this never ending loop as an example:
How will you stop this infinite massacre and save your CPU emotional damage? The answer’s quite simple. Use Ctrl + C.
Halting unwanted processes on screen
There are times when you may unwantedly start terminal programs, while you meant to do something else. At such times, you can use Ctrl + C to stop the process from being run.
Simply bored of stuff
Well, yeah. Most terminal programs have a different shortcut set to it for quitting the program. But whether or not you use it, you can still use the Ctrl + C shortcut to quit from that process, as long as the program does not bypass the signal.
Ctrl + C vs the kill command
Ctrl + C inside a terminal has only one goal in mind – to interrupt a process and then terminate it while it is being run. Ctrl + C does not require any command input to the terminal in order for it to work. You can use Ctrl + C even if there is no virtual terminal available for input commands for terminating it. However, don’t confuse it with the kill command. It’s more than just interrupting.
The kill command is set to terminate programs by default, but it can be used to run any other signals available on the system to a process. It is also quite common to include the kill() system call inside a terminal application’s code. On certain events, the process can execute this command to send itself signals that trigger a termination, pausing, or continuation of the process.
Ctrl + C vs Ctrl + Z | Are they the Same?
No, they are not. People ordinarily tend to confuse Ctrl + C and Ctrl + Z, as both of them seem to carry out a similar functionality, if not entirely the same. Indeed, both Ctrl + C and Ctrl + Z can interrupt a running process, but the task that follows differs.
Ctrl + C will terminate the process after it has interrupted and stopped the running process. On the other hand, Ctrl + Z will only pause the process temporarily, and send the running process to the background. The user is then able to run any other terminals processes in the meantime, and once done, the user can again bring the paused process back to the foreground, by means of using the fg command inside the terminal.
Hopefully, you should now be having a very clear understanding of what Ctrl + C does, inside a terminal. It wouldn’t be so wrong calling it The Terminator (but it isn’t as bad as that robot, ya know). It’s your best friend when you need to quit from a running process, both wanted and unwanted. But remember, Ctrl + C won’t copy any text from the terminal, at least directly. But there are programs which allow you to achieve that by means of bypassing the Ctrl + C for termination, and then remapping it to be used for copying stuff. Just to make you noted, if you don’t really know how to copy things from a terminal, try pressing Ctrl + Shift + C instead.