[buug] loops and pipe sinks are subshells, aaaargh

Ian Zimmerman itz at buug.org
Wed Aug 3 12:52:38 PDT 2011


After all these years with Unix I still don't know how to deal
with this annoying misfeature of the shell, without going into
ridiculous contortions:

 [30]matica:~$ foo=1 ; for a ; do foo=bar ; done
 [31]matica:~$ echo $foo
1

or

 [32]matica:~$ foo=1 ; echo bar | read foo
 [33]matica:~$ echo $foo
1

or finally, somewhat closer to my real application:

 [34]matica:~$ echo bar > baz
 [35]matica:~$ foo=1
 [36]matica:~$ cat baz | while read line ; do foo=$line ; done
 [37]matica:~$ echo $foo
1

Help?  I understand _why_ this is happening (see Subject), I want a
solution.  And a pony too!

-- 
Ian Zimmerman
gpg public key: 1024D/C6FF61AD
fingerprint: 66DC D68F 5C1B 4D71 2EE5  BD03 8A00 786C C6FF 61AD
Rule 420: All persons more than eight miles high to leave the court.



More information about the buug mailing list