I have written a zsh script to find some files, loop through and process them. Once it's over, I need to know some state set by a flag inside the loop. But the modified value of the flag does not get reflected outside the while loop.
So I tried the loop without the find. This time, the modified value of flag persists outside the loop also.
Thanks for an explanation and a workaround for this.
'find | while' loop
flag=false
find . -name "*.sh" | while read line
do
# check conditions, process the file
echo "flag = $flag"
flag=true
echo "flag = $flag"
done
echo "flag = $flag"
Output
flag = false
flag = true
flag = false
Simple while loop
i=1
flag=false
while [ $i -le 1 ]
do
echo "flag = $flag"
(( i++ ))
flag=true
echo "flag = $flag"
done
echo "flag = $flag"
Output
flag = false
flag = true
flag = true