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

Re: Stupid shell script question about "read"



On 3/2/06, Kevin B. McCarty <kmccarty@princeton.edu> 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.

What's happening here is that the pipe is causing a subshell to be
spawned, which is then parsing the command "read a b c".

> Posh and dash behave like bash in this respect, so I guess that this is
> not a bug, and that what zsh does is actually an extension.  So, what is
> the correct POSIX-compatible way to get "read" to work as I want?

Yeah, I would guess that zsh is doing something really funky to make
this work -- passing variables from a subprocess back up to the
parent, or not actually spawning a subprocess with the pipe, or
something like that.

I'm not sure of the POSIX way to use read in this manner, but I found
this on Google/A9:

http://linuxgazette.net/issue57/tag/1.html

The example he gives, with the < <() syntax, worked in bash, but not
in Debian or FreeBSD's /bin/sh.



Reply to: