PDA

View Full Version : [sh] while loop and a global variable


blah
June 18th, 2009, 01:58
When I prefix while loop with pipe sh eats my global variable assigned inside the loop body$ echo baz | while true; do foo=blah; echo $foo; break; done
blah
$ echo $foo

$So, it's visible inside loop but not outside. Why it's invisible only when as part of a pipe?$ while true; do foo=blah; echo $foo; break; done
blah
$ echo $foo
blah
$In zsh it's visible in both cases.

8-CURRENT r194410M amd64 here.

ale
June 18th, 2009, 08:40
AFAIK it's because pipes are executed in a different process.
For example, without using variables, what is your $PWD after running the following?
cd ~ ; cd /tmp/ | pwd