Live data from Hacker News

What typing ^D does on Unix (2009)

utcc.utoronto.ca

1–10 of 162 posts

Re: What typing ^D does on Unix (2009)

#2
As a corollary, typing ^D twice on a non-empty line will also send EOF on the input, allowing you to provide a program with input not ending with \n from the command line. I knew about this behavior, but not why it worked, but this article's explanation clarifies this behavior as well. The first ^D terminates the read() call, passing the line so far, without terminating \n, to the program. The second causes read() to return with 0 read bytes, which is EOF, as explained in the article.

Re: What typing ^D does on Unix (2009)

#4
post #3

huh. That's really interesting. I assume ^L is handled in the tty driver as well?

^L usually has no assigned action.

  $ stty -a | grep -F '^D'
  intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ;

  $ stty -a | grep -F '^L'
  $
Your Ctrl-L clear-and-refresh command is implemented in Bash (or rather GNU readline).

There is a "reprint" action, commonly bound to ^R:

  $ stty -a | grep rprnt
  eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
It doesn't clear the screen; it just issues a newline and reprints the characters buffered so far. You can use stty to assign ^L to "rprnt".

Re: What typing ^D does on Unix (2009)

#8
post #3

huh. That's really interesting. I assume ^L is handled in the tty driver as well?

An important upshot is that TTY I/O never actually ends; EOF from a TTY is a temporary condition, and input can continue.

In the C standard library, you just clearerr(stdin) and keep reading.

If a program takes two files as input and obeys the - convention for specifying stdin (or has some other way of opening the tty more than once), you can do:

  $ program - -
then give it two inputs, each followed by [Ctrl-D][Enter]. For instance "cat - -".

This sort of thing subtly breaks with some utilities. Try coaxing GNU diff to compare two pieces of tty input, haha.

Re: What typing ^D does on Unix (2009)

#9
post #7

Pressing enter then ~. will gracefully shutdown hung ssh sessions

nice one, never seen that - always used ^D/exit

Those are yet more graceful -- for when the shell is responsive. Use ~. in cases where for some reason you cannot get access to the terminal anymore but remote sshd is responsive.

Re: What typing ^D does on Unix (2009)

#10
post #9
post #7

Earlier quoted context omitted.

nice one, never seen that - always used ^D/exit

Those are yet more graceful -- for when the shell is responsive. Use ~. in cases where for some reason you cannot get access to the terminal anymore but remote sshd is responsive.

iirc, enter-~-. isn't anything to do with the remote sshd. Instead, it escapes to the local ssh client (enter-~) that it should close (.). Which is really useful for if the remote sshd blew up, or, more commonly, your connection was broken.
Post reply on HN