[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: read not terminated with EOF/^D when -n or -N is used

From: Stephane CHAZELAS
Subject: Re: read not terminated with EOF/^D when -n or -N is used
Date: Mon, 27 Dec 2010 10:54:07 +0000 (UTC)
User-agent: slrn/pre1.0.0-18 (Linux)

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

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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]