Honestly I had no idea what ctrl+d even did, I just knew it was a convenient way for me to close all the REPL programs I use. The fact that it is similar to pressing enter really surprised me, so I wanted to share this knowledge with you :)
Linux Terminal: CTRL+D is like pressing ENTER
https://hackarcana.com/article/ctrl-d-is-like-enter
13 Comments
davel@lemmy.ml · 25 pts · 1y
CTRL+M is like pressing ENTER. Kernigan & Pike, 1984: UNIX Programming Enviornment
tuna@discuss.tchncs.de · 11 pts · 1y
This reminds me of a time at work when I was not on a reasonable terminal. I was explaining to a co-worker how I automated some tasks by running some scripts, but in my demo my RETURN key didn't work, so I had to improvise and use CTRL+M which worked, hahaha. I don't know how the terminal got in such a bad spot but it was probably something to do with msys on Windows.. honestly not sure. It was perfect timing to have happen while teaching of course ;)
I would also be doing a disservice not to share what the book you linked says about CTRL+D. Right after your quote, it says:
This is pretty good for an introduction, but it is not the full story. It explains CTRL+D properly later (chapter 2, page 45):
This is why the article says it's "like pressing enter," because it flushes the input just like enter. The difference is that enter sends a newline, but CTRL+D does not, so you can exploit that to send no data (and the program chooses to interpret that as an EOF).
Ferk@lemmy.ml · 7 pts · 1y
Yes, although
Ctrl-Mwould be the "Carriage Return" character (\r). For the "Line Feed" newline character (\n) the Control combination would beCtrl-J. Both of them would normally produce a new line when you press them on most terminals.That's why if you open in nano/vim a file with Windows style EOL (
/r/n), you might see a strange^Msymbol at the end of each line.mina86@lemmy.wtf · 21 pts · 1y
cypherpunks@lemmy.ml · 8 pts · 1y
Note: for readers who aren't aware, the notation
^Xmeans hold down the ctrl key and type x (without shift).ctrl-a though ctrl-z will send ASCII characters 1 through 26, which are called control characters (because they're for controling things, and also because you can type them by holding down the control key).
Nope, Chuck Testa: there is no EOF character. Or, one could also say there is an EOF character, but which character it is can be configured on a per-tty basis, and by default it is configured to be
^D- which (since "D" is the fourth letter of the alphabet) is ASCII character 4, which (as you can see inman ascii) is called EOT or "end of transmission".What that
sttyoutput means is that^Dis the character specified to triggereof. That means this character is intercepted (by the kernel's tty driver) and, instead of sending the character to the process reading standard input, the tty "will send an end of file (terminate the input)".By default
eofis^D(EOT), a control character, but it can be set to any character.For instance: run
stty eof xand now, in that terminal, "x" (by itself, without the control key) will be the EOF character and will behave exactly as^Ddid before. (The rest of this comment assumes you are still in a normal default terminal where you have not done that.)But "send an end of file" does not mean sending EOT or any other character to the reading process: as the blog post explains, it actually (counterintuitively) means flushing the buffer - meaning, causing the
readsyscall to return with whatever is in the buffer currently.It is confusing that this functionality is called
eof, and thesttyman page description of it is even more so, given that it (really!) does actually flush the contents of the buffer toread- even if the line buffer is not empty, in which case it is not actually indicating end-of-file!You can confirm this is happening by running
catand typing a few characters and then hitting^D, and then typing more, and hitting^Dagain. (Each time you flush the buffer,catwill immediately echo the latest characters that had been buffered, even though you have not hit enter yet.)Or, you can pipe
catintopvand see that^Dalso causespvto receive the buffer contents prior to hitting enter.I guess unix calls this
eofbecause this function is most often used to flush an empty buffer, which is how you "send an end of file" to the reader.The empty-
read-means-EOF semantics are documented, among other places, in the man page for theread()syscall (man read):If you want to send an actual
^D(EOT) character through to the process reading standard input, you can escape it using the confusingly-namedlnextfunction, which by default is bound to the^Vcontrol character (aka SYN, "synchronous idle", ASCII character 22):Try it: you can type
echo "and then ctrl-V and ctrl-D and then"|xxd(and then enter) and you will see that this is sending ascii character 4.You can also send it with
echo -e '\x04'. Note that the EOT character does not terminate bash:As you can see, it instead interprets it as a command.
::: spoiler (Control characters are perfectly cromulent filenames btw...)
:::
mina86@lemmy.wtf · 2 pts · 1y
Ferk@lemmy.ml · 8 pts · 1y
To be more precise, it's the "EOT" (end of transmission) control character, the 4th symbol in ASCII, from the non-printable character area.
double_quack@lemm.ee · 17 pts · 1y
Ctl-D is the End-of-File character. Programs interpret it as "that's it, the input you were reading has finished", and react accordingly.
tuna@discuss.tchncs.de · 1 pts · 1y
The Ctl-D didn't end the file when i typed "Bye" :( it only worked when I pressed Ctl-D on its own line. So how does cat know that it should ignore the EOF character if there is some text that comes before it?
What Ctl-D does is flush the input to the program, and the program sees how big that input is. If the length of the input is 0 that is interpreted as EOF. So Ctl-D is like Enter because they both flush the input, but Ctl-D is unlike Enter because it does not append a newline before flushing, and as a consequence you can send empty input (aka an EOF "character") with Ctl-D.
skepller@lemmy.world · -1 pts · 1y
This!
It's merely a buffer flush, in case it's empty, the program handling the input can choose how to interpret,
catdecides to do it as an EOF.Reason why it also works as exit.
ramius345@sh.itjust.works · 9 pts · 1y
Ctrl+d terminates input on stdin to your currently running program or shell.
tuna@discuss.tchncs.de · 3 pts · 1y
not true. try this:
bash did not terminate stdin, because when i press enter it still runs the command, and my shell continues to work as normal!
you can also try this:
and it will print the date.
so something else is happening here! thats what the link talks about in detail
ramius345@sh.itjust.works · 1 pts · 1y
For some reason my mobile client didn't make the article link immediately obvious. That's actually really interesting. Apparently I was under the same common misconception. So the shell in this case is choosing to continue after detecting the flush.
tuna@discuss.tchncs.de · 1 pts · 1y
Ohh I gotcha. Honestly no sweat, its kind of just a bit of fun trivia really :)
NeoNachtwaechter@lemmy.world · 1 pts · 1y
mvirts@lemmy.world · 6 pts · 1y
Lol wrong again