34
submitted 3 days ago* (last edited 3 days ago) by mina86@lemmy.wtf to c/linux@lemmy.ml

Response to a recent claim that Ctrl+D in the terminal is like pressing Enter. It kind of is but it’s also misleading to say so without further explanation.

you are viewing a single comment's thread
view the rest of the comments
[-] MyNameIsRichard@lemmy.ml 25 points 3 days ago

Ctrl+D sends EOF, so no it's not like enter.

[-] mina86@lemmy.wtf 11 points 3 days ago

Yes, I agree. But the dispute is what ‘sends EOF’ actually means. The article I respond to claims Ctrl+D doesn’t send EOF but is like Enter except that new line character is not sent. This is, in some sense true, but as I explain also misleading.

[-] AndrasKrigare@beehaw.org 2 points 3 days ago

To me, the potential point of confusion is referring to "sent by Ctrl+D" and things "received by the end process" as synonymous, ignoring the tty driver in between. When you Ctrl+d, you send a magic byte value to the tty master (which I would refer to as a EOF character, but I understand the argument against the terminology). On the other side of it the process doesn't receive this value, but instead has its read call returned even if the buffer is 0.

A simple example hopefully highlighting the difference

Window1:
nc -nvlp 5555 #"far nc"

Window2:
nc -nv 127.0.0.1 5555 #"local NC"
Hi there[Enter]
Hi [Ctrl+D]There[Ctrl+D][Enter]

Window3:
strace -p [pid of local nc]

Window2:
[Right arrow][Right arrow][Ctrl+D]
[Ctrl+D]Uh oh[Enter]

What we see is pretty much as described. From the first line, we see "Hi there\n" on the other side. For the second line, we first see "Hi " appear, then "There" then "\n".

From the third line, in the strace we can see the sequences representing the right-arrow key, and we can see the tty driver on the far side takes those sequences and interprets them to render the cursor two characters to the right.

The fourth line is where it gets more interesting. We send the tty driver the EOF byte, and the tty driver interprets this and gives the current active tty client a 0-byte return to read() and assumes we have no more data to send. But unlike bash, nc doesn't care about a 0-byte read and is still looking for more data (as we can see in the strace). But if we continue to type and send more data (the "Uh oh"), we can see in the strace that the tty never sends this to the nc. So, to some definition, we're still sending data to the local nc, but the tty driver isn't actually relaying it

this post was submitted on 30 Mar 2025
34 points (92.5% liked)

Linux

52706 readers
546 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS