On Thu, 18 Jan 2001, Hal Burgiss wrote:

> 
> Is the below expected behavior? or bug? Values assigned to variables
> within the loop, are not visible outside the loop. Using something
> like 'while true' works as I would expect.
> 
> 
> #!/bin/bash
> ## script: testing
> ## test variable visibility
> num=0
> echo 1 > tmp
> cat tmp |while read line ; do 
>  num=1
>  echo $num
> done
> echo $num
> 
> #--- eof testing

The reason is because the "while" loop is run in a
sub-process and any variables assigned there are lost...
but there is another way as follows: 

#!/bin/bash
## script: testing
## test variable visibility

num=0
echo 1 > tmp

while read line
do
    num=1
    echo $num
done < tmp

echo $num

The above will accomplish the same thing by redirecting
stdin from "tmp" for the "while" loop, preventing a fork. 

Remember that a "|" will always cause a subprocess to be
forked

-- 
John Darrah (u05192)    | Dept: N/C Programming
Giddens Industries      | Ph: (425) 353-0405 #229
PO box 3190             | Ph: (206) 767-4212 #229
Everett  WA    98203    | Fx: (206) 764-9639



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to