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

Re: Stupid shell script question about "read"



On Thu, Mar 02, 2006 at 10:23:20AM -0500, Kevin B. McCarty wrote:
> Hi list,
> 
> Could someone tell me why the following works in zsh but not in
> bash/posh/dash?
> 
> benjo[3]:~% echo foo bar baz | read a b c
> benjo[4]:~% echo $a $b $c
> foo bar baz
> 
> If I try the same with bash (or other sh-compatible shells), the
> variables $a $b and $c are unset.  From the bash man page:

[...]

> So "read" claims to read from the standard input, but it doesn't
> actually seem to happen when a pipe is involved.

It does happen.  However, see the section "Pipelines" in the bash info
page:

    A `pipeline' is a sequence of simple commands separated by `|'.
    [...]
    The output of each command in the pipeline is connected via a pipe
    to the input of the next command.  That is, each command reads the
    previous command's output.
    [...]
    Each command in a pipeline is executed in its own subshell
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For some builtin commands this may be useful; for 'read' obviously it is
not.



Reply to: