It's partially explained Stéphane Chazelas's answer, so I'll try to quote the relevant parts:
Now, if the current line was empty, and provided the application will
have fully read the previously entered lines, the read
will return 0
character.
That signifies end of file to the application (when you read from a
file, you read until there's nothing more to be read). That's why it's
called the eof
character, because sending it causes the application
to see that no more input is available.
Now, modern shells, at their prompt do not set the terminal in
icanon
mode because they implement their own line editor which is
much more advanced than the terminal driver built-in one. However, in
their own line editor, to avoid confusing the users, they give the
^D
character (or whatever the terminal's eof
setting is with some)
the same meaning (to signify eof
).
That last line can use a bit more clarification. From the documentation of Bash's line editor, readline:
end-of-file
(usually C-d)
The character indicating end-of-file as set, for example, by stty
.
If this character is read when there are no characters on the line,
and point is at the beginning of the line, Readline interprets it as
the end of input and returns EOF.
Hence, for readline (and also for most other shells), Ctrl-D is only EOF at the start of a line. So, even if you did manage to add Ctrl-D at the end of that line, it won't be treated as EOF.
And whether EOF will make your interactive shell exit is also a different problem. For bash, this is governed by the IGNOREEOF
variable and the set -o ignoreeof
option. The variable's documentation says:
Controls the action of the shell on receipt of an EOF character as the
sole input. If set, the value denotes the number of consecutive EOF
characters that can be read as the first character on an input line
before the shell will exit. If the variable exists but does not have a
numeric value, or has no value, then the default is 10. If the
variable does not exist, then EOF signifies the end of input to the
shell. This is only in effect for interactive shells.
Example:
bash-5.0$ IGNOREEOF=
bash-5.0$ <Ctrl-D> Use "exit" to leave the shell.
bash-5.0$ <Ctrl-D> Use "exit" to leave the shell.
bash-5.0$ <Ctrl-D> Use "exit" to leave the shell.
bash-5.0$
Do it 7 more times and it will finally exit.
I don't really see any use for this. If you want to exit the shell at the end of these commands, just use exit
.
ls;pwd;
followed byCtrl-D
(instead ofEnter
), nothing special happens. Do you want to exit the shell after running those commands? – Kusalananda Dec 11 '19 at 12:03