The shell will read commands from your terminal (i.e. the shell's standard input) until it's told to stop (by entering the
exit command) or when an end-of-file (EOF) is encountered (just like when the shell is executing a script).
If you enter the
exit command, this will first be saved into the history, and then the shell will quit. Entering ctrl-D generates an EOF; as this is not a command but an input state, it can't be saved in the shell's history.
Note that you can instruct
bash to ignore EOF; this can be done in 2 ways:
- set -o ignoreeof
The first way is equivalent to doing
IGNOREEOF=10, meaning you have to hit ctrl-D 10 times in a row before the shell will exit. This is to prevent accidentally terminating the shell e.g. if you mistype ctrl-C or whatever.
Note also that ctrl-D only generates an
EOF when entered as the first thing on a line (i.e. after an enter),
bash ignores it if anything has already been typed on the line.
The ctrl-D also generates
EOF outside the shell. E.g.:
1 2 8
wc has counted 1 line, 2 words, 8 characters (including the newline).
Here's an example with using ctrl-D after some input has been given on the line:
foo bar<ctrl-D><ctrl-D> 0 2 7
wc has counted zero lines (no
enter had been given), 2 words and 7 characters. Two ctrl-D sequences were necessary because data had already been given; note that
bash handles this specially (ignoring the
EOF if something has already been entered on the line), whereas e.g.
dash doesn't. This is probably due to the line editing facility in