3

If I press C-d at the end of the input, the input is seemed to be flushed, but no EOF is sent to the program. If however I press C-d once more, EOF is sent.

Why is the EOF not sent the first time I press C-d? Where is this behavior documented?

To experiment youself play with cat, here is an example:

$ cat
abc<C-d>abc<C-d>
$
Roman Byshko
  • 8,591
  • 7
  • 35
  • 57
  • This is quite helpful http://stackoverflow.com/questions/1516122/how-to-capture-controld-signal/1516177#1516177 – kaskelotti Dec 12 '12 at 12:06

2 Answers2

4

C-d is the End-of-transmission (EOT) character:

In Unix the end-of-file character (by default EOT) causes the terminal driver to make available all characters in its input buffer immediately; normally the driver would collect characters until it sees an end-of-line character. If the input buffer is empty (because no characters have been typed since the last end-of-line or end-of-file), a program reading from the terminal reads a count of zero bytes. In Unix, such a condition is understood as having reached the end of the file.

In your example, when you run cat and type a few characters followed by C-d, the characters typed to that point are sent to cat which prints them to the screen. Now the input buffer is empty, so when you type C-d again, cat reads zero bytes from the buffer and terminates because the end-of-file condition has been met.

dogbane
  • 266,786
  • 75
  • 396
  • 414
0

With bash you're probably using the readline library.

As you asked for the documentation, here it is

You'll see that Ctrl-D deletes the character underneath the cursor, and if you keep on reading, you'll discover lots of wonderful shortcuts!

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • It is wrong I think. After issuing a command I'm not in readline anymore. That is why I should also adjust the title. – Roman Byshko Dec 12 '12 at 13:40
  • @Beginner, of course you're using readline! after typing `abc` you have no character _underneath_ the cursor so Ctrl-D will not do anything. But if you go back one or two characters with the left arrow key, try Ctrl-D, it will indeed erase the character _underneath_ the cursor. Please also read [section 8.4.3](http://www.gnu.org/software/bash/manual/bashref.html#Commands-For-Text) from the manual for the special case of a Ctrl-D issued at the beginning of an empty line. – gniourf_gniourf Dec 12 '12 at 17:09
  • Maybe we undestand each other, so here the key sequence I type in: ``catabc``. What I see is afterwards actually this: ``catabc^[[D``. So it does not allow me to go left. Those are the default settings on Arch and Debian. – Roman Byshko Dec 12 '12 at 23:05