Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!hubcap!ncrcae!ncr-sd!hp-sdd!hplabs!hpda!hpclla!hpcllf!jws From: jws@hpcllf.HP.COM (John Stafford x75743) Newsgroups: comp.unix.questions Subject: Re: variables set in sh while loop don't get set? Message-ID: <4210001@hpcllf.HP.COM> Date: Mon, 28-Sep-87 20:44:08 EDT Article-I.D.: hpcllf.4210001 Posted: Mon Sep 28 20:44:08 1987 Date-Received: Thu, 1-Oct-87 04:08:48 EDT References: <212@ttrdd.UUCP> Organization: Hewlett-Packard CLL Lines: 26 Shell script loops with any I/O of the loop as a whole redirected (i.e. stdin in your case) are run in a separate (sub-)shell process and so changes in variables in such loops obviously are not reflected in the parent. Loops without redirected I/O are run in the current shell and so changes in variables work. This is either not documented or obscurely documented. I have "gotten around" this feature by writing things into temporary files and having the parent get them from there: # Replace VARIABLE="" with temp=/tmp/x$$ trap "rm -f $temp" 0 1 2 3 15 # Don't leave a mess cp /dev/null $temp # >$temp would work also process | while read something do # Replace VARIABLE="$VARIABLE $something" with echo " $something" >>$temp done VARIABLE="`cat temp`" It isn't perhaps the cleanest, but it works. John Stafford -- Hewlett Packard Computer Language Lab {allegra,decvax,ihnp4,ucbvax}!hplabs!hpclla!jws {fortune,sun,thirdi,ucbvax} !hpda !hpclla!jws