I am having some issues with local variables after exiting a loop. The variable max ends up with the value 0, despite the code below:
max=0
cat tmp|while read line
do
temp=$(echo $line|tr -d "\n"|wc -c)
if [ $temp -gt $max ];then
max=$temp
echo $max
fi
done
echo -n tmp $max
cat tmp
12345
123456
And this is the output I receive:
5
6
tmp 0
I don't understand why max is 0 after exiting the loop, while inside the loop it finds the correct values.