According to the bash man page each command in a pipeline is executed in a subshell. That is, your while loop runs in a subshell and only the value of the variable max in that subshell is modified.
Variables of subshells are not propagated back to the calling shell, which is running the echo command and thus still sees the initial zero value.
If you run the echo in the same subshell (note the curly braces) it will work:
max=0
cat tmp|{
while read line
do
temp=$(echo $line|tr -d "\n"|wc -c)
if [ $temp -gt $max ];then
max=$temp
fi
done
echo -n tmp $max
}
If you need the value for further calculations in the outer shell, you would need to use command substitution like this:
max=0
max=$(cat tmp|{
while read line
do
temp=$(echo $line|tr -d "\n"|wc -c)
if [ $temp -gt $max ];then
max=$temp
fi
done
echo -n $max
})
echo tmp $max