bug-bash
[Top][All Lists]
Advanced

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

parent shell/sub-shell puzzlement


From: Ian Macdonald
Subject: parent shell/sub-shell puzzlement
Date: Tue, 4 Feb 2003 18:21:11 -0800
User-agent: Mutt/1.4i

Hi,

I'm puzzled by the the behaviour of the following piece of code:

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.

Here's the output:

address@hidden ianmacd]$ bash ./testing
In loop: foo bar baz fiz
Outside loop: fiz

>From the fact that the value of $four is printed in the final echo, it's
clear that $four is being defined in the main shell, not in a subshell.
This suspicion is confirmed by placing an 'exit' in the body of the
while loop. If I do that, the script exits immediately, which obviously
wouldn't happen if the while loop were executing in a subshell (i.e.
only the subshell would exit).

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?

If the 'read' is causing a subshell to be spawned, then $one, $two and
$three would be defined in that subshell and would not be echoed
properly within the while loop, but they are.

So what, exactly, is going on in this snippet of code?

How come $one, $two and $three are not set in the main shell?

Ian
-- 
Ian Macdonald               | A day for firm decisions!!!!!  Or is it? 
address@hidden             | 
                            | 
                            | 
                            | 




reply via email to

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