Linux terminals are hilariously efficient and technically marvelous when you need to carry out repetitive jobs. The ability to automate commands and run them the way you want is one of the best things about using terminals rather than using a mouse in a graphical user interface. Yes, you’d don’t get the graphical cherries you get on top of GUI programs, but using a terminal for all things repetition is the best thing since sliced bread.
One of the important keyboard shortcuts one might use in a Linux terminal is, Ctrl + D, which doesn’t serve a single purpose only. It will work in different ways based on the situation you are in. It’s pretty much like a brother to Ctrl + C, however not having the same goal in mind.
You don’t need to take my words for it. The spilled beans in sections that follow should be enough to give you an idea about the use of Ctrl + D in a Linux terminal.
What signal does Ctrl + D send?
Ctrl + D does not send any signal, to be very exact. It rather sends an input to the terminal, which is EOF. It’s more of a programming thing, and unlike much of its bros like Ctrl + C and Ctrl + Z, it has a more distinctive use case, more specifically, in programming. But one thing to be kept in mind is that this keyboard shortcut only works if your terminal is reading inputs, when it prompts you to write or do something.
What is EOF in Ctrl + D?
The EOF in Ctrl + D is short for End of file, which is an important concept in programming, as well as in terminal shells. Now there are a lot of heavy concepts put onto this on the Internet, but my target here is to help you understand what it actually does with as simple terms as possible.
What you need to focus here is not what EOF does in programming, but rather, inside a terminal shell. There are two things that’ll happen if you press Ctrl + D on your keyboard, based on the situation. The first situation is that you’ve just typed a command in your terminal, and you haven’t pressed Enter to execute the command yet. Now, if you press Ctrl + D, the command you had typed out will be executed.
Quite strange, isn’t it? But that’s not just the end yet, despite being “End of file”. There’s more mantra to it. Now quit the program or the process, and then return to the terminal. At this very moment, if you haven’t typed anything in the terminal, and try pressing Ctrl + D, the terminal exits.
No comments. Period.
But what does it even have to do with the concept of EOF? Good question. The following section will try to explain it all, without breaking your head in any means.
Why does Ctrl + D exit the terminal?
If you know at least the basics of programming, you should be familiar with the terms STDIN and STDOUT. STDIN is short for “Standard Input”, and it’s used for referring to an input the user gives to the computer by means of pressing the keyboard or by clicking buttons using a mouse. It is not limited to pressing stuff, however. In fact, a more common form of STDIN is a string of text, or a piece of command sent to the computer or a particular program.
This input from the user is read by the program using a function, such as the read() function found in some programming languages. Now let’s use this knowledge inside a terminal to better understand the function of Ctrl + D.
End of File does not really depict that a file is ending or whatever. Despite its name, there is something more technical about it. Let us not focus on that, however. What you should know instead is that when you enter the terminal, it prepares the read() function, meaning that it is ready to take any input you give to it. Now if you type in a command, like the one below:
$ echo Hello World
and then press Enter, the typed command will be sent to the read() function, which should end up looking like this “read(echo Hello World)”, even though you’ll not actually see it, that’s how the language will interpret it when being executed.
So far so good. But we’ve come no close to Ctrl + D yet, or so it seems. Just like the demonstration in the previous section, if you press Ctrl + D instead of pressing Enter in the above example, it’ll execute the command the same way Enter would do:
Here in this case, Ctrl + D is being taken as the “End of file” for the read() function, or more plainly, it is working as “End of Input”. Did you catch the trick here? After pressing Ctrl + D, you’re telling the terminal:
“Ok pal, I’m done typing my input, so I’m pressing Ctrl + D. Now execute the command”.
I could be no clearer than this.
Hold on. Let’s say that you haven’t typed any command and directly pressed Ctrl + D. Why does the terminal emulator exit? This is because when you’ve typed no command while the terminal is asking you for an input through the read() function, you’re giving it an empty value, which is translated as a Zero for the function, and should end up looking like read(0). This time around, what you’re telling the terminal is this:
“Comrade, I’ve no order left to give you. So you are now free to leave”.
Getting the idea yet? When you press Ctrl + D after typing a command in the terminal, you’re actually asking the terminal to do something, so it executes the command. But when you haven’t typed a command and have directly pressed Ctrl + D, you’re not giving any command, but rather an empty reply, so the terminal will take it like an End of File, and quit.
Ctrl + D in a shell script
You cannot directly type out Ctrl + D inside a shell script to make it work the way it does. Rather, you’ll have to use something else to achieve a similar functionality.
There are two reasons why you may want to achieve this. The first being that when you execute a bash script that you’ve created, you want the terminal to quit once the execution of the script is over. A better way to achieve this is to use the “kill -9 $PPID” command in the end of your script instead. Take the following script as an example:
#!/bin/bash sleep 5 kill -9 $PPID
As soon as the soon command is over, the kill command will ask the terminal to terminate itself.
The second reason why you may wish to use Ctrl + D in a terminal is to use it as a method for you to run a different set of commands, when you press Ctrl + D. To do this, first you’ll need to capture the Ctrl + D input and warp it inside a logical sequence, which in turn will execute further commands. To help you better clarify the use, take this script as an example:
#!/bin/bash sleep 5 echo “sleep is over. Now press ctrl + d” read user_input # The script receives Ctrl + D input as "1" if [[ $? == 1 ]] then echo “you’ve pressed ctrl + d, the terminal will now exit” sleep 3 kill -9 $PPID fi
Ctrl + C vs Ctrl + D
Ctrl + C and Ctrl + D are totally different things, so try not to confuse the two. Ctrl + C will terminate a running program, while Ctrl + D will quit the terminal if there is no other input to be processed. Also, if you have an input typed out, pressing Ctrl + D will do the same job as Enter, essentially executing the command you typed.
To sum up, Ctrl + D is actually a tough concept to grasp your head around, if programming is not your thing yet. Using Ctrl + D in a shell script is not as straightforward as pressing Ctrl + D, but you can emulate that by means of using the read command to take an input while running the bash script, and then press Ctrl + D to assign a series of other commands to be execute. Note the fact that Ctrl + D inside the script is taken as a 1 in the read command, which is why “1”, is passed as a condition for the if statement. Ctrl + D, is definitely on the roll!