Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!hp4nl!targon!andre
From: andre@targon.UUCP (andre)
Newsgroups: comp.unix.questions
Subject: Re: Bourne Shell FOR loop confusion
Keywords: why does it do this
Message-ID: <593@targon.UUCP>
Date: 14 Aug 89 10:27:06 GMT
References: <689@msa3b.UUCP>
Reply-To: andre@targon.UUCP (andre)
Organization: Nixdorf Computer BV., DO, P.O. Box 29,Vianen, The Netherlands
Lines: 36

In article <689@msa3b.UUCP> kevin@msa3b.UUCP (Kevin P. Kleinfelter) writes:
>When I enter the following:
>    y=y
>    for i in abc
>    do
>       y=$i
>    done < /dev/null
>    echo $y
>The output is "y", which is exactly what I want to know! ("why").

As long as a loop in the shell uses the stdin stdout and stderr of
that shell, the loop is executed by that shell, your loop however
redirects its stdin. The shell solves this by spawning a subshell that
has its stdin redirected. Your second loop is thus executed by a subshell
and the value of y stays "y".
If you echo the value of y inside the loop you see the right value, but
that value cannot be transferred back to the parent shell. Maybe you
can try something like,

    y=y
    y=`(
	for i in abc
	do
	   y=$i
	done
	echo $y
       ) < /dev/null `

I let the