Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 (Tek) 9/12/84; site tekcrl.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!bellcore!decvax!tektronix!tekcrl!terryl From: terryl@tekcrl.UUCP () Newsgroups: net.unix Subject: Re: why doesn't this shell program work Message-ID: <69@tekcrl.UUCP> Date: Mon, 4-Mar-85 14:53:06 EST Article-I.D.: tekcrl.69 Posted: Mon Mar 4 14:53:06 1985 Date-Received: Thu, 7-Mar-85 05:46:21 EST References: <2147@drutx.UUCP> Lines: 30 >variable=none >echo "directories" >for filename >in `ls` >do > if test -d $filename > then variable=some > echo $filename > fi >done | pr -t4 >echo $variable > >HOWEVER, variable will always be none wheather or not anything >was found! further debugging shows that variable = some before >exiting the pipe but, variable = none after exiting the pipe. >Why does the pipe do this? Because of the pipe, the shell has to fork off two processes for both sides of the pipe, i.e. the for loop and the pr command. So, what is in reality happening is that there are three processes running: the original process for the shell script, the first subprocess for the for loop, and the second sub- process for the pr command. Since you are executing the for loop in a sub- process, all variables set inside the for loop only have their true value as long as the for loop is running. If you take out the pipe, then things work as you expected. Terry Laskodi of Tektronix