Local variables after loop exit

bash

Solution

A pipe starts a new subshell, with its own environment and variable space. Use `< tmp` at the end of the loop instead.

Problem

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.

Original source