bug-bash
[Top][All Lists]
Advanced

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

Re: parent shell/sub-shell puzzlement


From: Ian Macdonald
Subject: Re: parent shell/sub-shell puzzlement
Date: Wed, 5 Feb 2003 10:53:49 -0800
User-agent: Mutt/1.4i

On Wed 05 Feb 2003 at 13:46:43 -0500, you wrote:

> Ian Macdonald <address@hidden> wrote:
> > while read one two three; do
> >       four=fiz
> >       echo In loop: $one $two $three $four
> > done < <(echo foo bar baz)
> > echo Outside loop: $one $two $three $four
> >
> > As far as I can see, $one, $two, $three and $four are defined at the
> > same level. I would expect the while loop to print all four values on
> > one line and the outer echo to print nothing.
> ...
> > However -- and now I'm getting to the source of my puzzlement -- if the
> > body of the while loop is, in fact, executed in the main shell, why are
> > the values of $one, $two and $three not also set in the main shell?
> 
> The first time through the loop, read assigns the values foo, bar, and
> baz to the variables one, two, and three.  The second time, read sees
> EOF, so as described in the man page:
>               If there are fewer words read from the input stream than
>               names, the remaining names are assigned empty values.
> In this case, 0 words are read from the input stream, so all the
> variables are set to the empty string.  But there is nothing to reset
> four.

The explanation turned out to be more mundane than I expected. It didn't
occur to me that there would be two reads, the second for EOF.

Thanks,

Ian
-- 
Ian Macdonald               | One planet is all you get. 
address@hidden             | 
                            | 
                            | 
                            | 




reply via email to

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