When you're entering input into a program at the Posix command line, and want to end the input, enter Ctrl-D on an otherwise empty line.
Your terminal is in cooked mode, essentially a line editor for each command. Your program won't receive input until the line is "entered" with the enter/return key. If the line you're editing is not empty, Ctrl-D will cause it to be sent to the terminal input's destination (your program) without the newline that acompanies the enter/return key.
With an empty line buffer, Ctrl-D is how the operator informs the shell to apply the end-of-file condition to the input stream attached to the keyboard.
Consider this example with cat
, a program which in this invocation writes its input stream to its output stream, not unlike the question's program.
$ cat
Usually, I can edit a line - using backspace to edit the input before I enter it. Now I will press enter
Usually, I can edit a line - using backspace to edit the input before I enter it. Now I will press enter
Mid-line, I can press Ctrl-D to send the line. I'll press it now: Mid-line, I can press Ctrl-D to send the line. I'll press it now:
I pressed enter after that was output, rendering both the newline I typed, echoed to the terminal, and the newline cat received.
I pressed enter after that was output, rendering both the newline I typed, echoed to the terminal, and the newline cat received.
But, at an empty line buffer, Ctrl-D ends input altoghether. I'll press return, and then Ctrl-D.
But, at an empty line buffer, Ctrl-D ends input altoghether. I'll press return, and then Ctrl-D.
When I press enter/return, the line I've been editing is sent to the terminal's destination (cat) - including the newline. When I press Ctrl-D mid-line, the line I've been editing is sent to the destination without the newline. Ctrl-D on an empty line (you may have typed, then deleted, but now the line is empty) tells the input reader that there's no more data, just like when you read from a file when your position is at the end already.