2010-12-26, 18:19(-05), Isaac Good: [...] > read <ENTER><^D> -> the EOF terminates read and it exists with a false > exist status. > read -n4 <ENTER><^D> -> the ^D is read by read and stored in REPLY. read > does not terminate.
It's not bash that handles the ^D, it's the terminal. When in canonical mode, upon pressing ^D, the terminal emmits "EOF" (a read(2) returns). Though it's not documented, when using "-n", read puts the terminal in raw mode. In that mode, the ^D is not special (it is sent as is, so read by bash's read). In zsh (where -n is -k), that behavior (putting the terminal in raw mode) can be disabled by adding a "-u" flag, as in -u0 to specify the fd to read from. It doesn't look like bash has a similar feature, so you'd have to do: read -n4 < <(cat) for instance, so that read sees its stdin as a pipe and not a terminal. Note that when in canonical mode, the terminal only sends characters to the application (here cat) upon pressing <Enter> or <Ctrl-D>, so read won't return after you enter 4 characters. -- Stephane