Actually it's not the loop which retains local variables: it is the pipe redirection which opens a subshell that uses local variables
Code:
head -n500 esn.txt | while ...
do
#
# this is the subshell scope due to pipe redirection
#
done
When using "process substitution" (as posted by rupertwh) a new subshell is not called and the modified variables are retained outside the block of code. Another way to do the same is to link a file descriptor to the input file (or part of it if using the command head with process substitution). The option -u of read let you take input from the specified descriptor:
Code:
#!/bin/bash
counter=0
exec 3< esn.txt
while read -u 3 line
do
let "counter += 1"
if [ "$counter" -eq 1 ]
then
email=${line:55}
echo $email
elif [ "$counter" -eq 4 ]
then
ESN=$line
echo $ESN
fi
done
echo $ESN
echo $email
exec 3<&-