Bourne Shell FOR loop confusion
andre
andre at targon.UUCP
Mon Aug 14 20:27:06 AEST 1989
In article <689 at msa3b.UUCP> kevin at 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 </dev/null be there to show where you must put the redirection now.
--
\---| AAA DDDD It's not the kill, but the thrill of the chase.
\ | AA AAvv vvDD DD Ketchup is a vegetable.
/\ \ | AAAAAAAvv vvDD DD {nixbur|nixtor}!adalen.via
_/__\__\| AAA AAAvvvDDDDDD Andre van Dalen, uunet!hp4nl!targon!andre
More information about the Comp.unix.questions
mailing list