By default, macOS (formerly OS X and Mac OS X) software recognizes EOF
when Ctrl-D is pressed at the beginning of a line.
In detail, the actual operation is that, when Ctrl-D is pressed, all bytes in the terminal’s input buffer are sent to the running process using the terminal. At the start of a line, no bytes are in the buffer, so the process is told there are zero bytes available, and this acts as an EOF
indicator.
This procedure doubles as a method of delivering input to the process before the end of a line: The user may type some characters and press Ctrl-D, and the characters will be sent to the process immediately, without the usual wait for enter/return to be pressed. After this “send all buffered bytes immediately” operation is performed, no bytes are left in the buffer. So, when Ctrl-D is pressed a second time, it is the same as the beginning of a line (no bytes are sent, and the process is given zero bytes), and it acts like an EOF
.
You can learn more about terminal behavior by using the command “man 4 tty” in Terminal. The default line discipline is termios. You can learn more about the termios line discipline by using the command man termios
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…