I have written a shell scritp in which i am using a variable which is declared before a while loop and i am updaitng the variable in while loop and want to use its updated value outside the loop. I am not able to do so, b'coz the scope of the variable is limited to the while loop only and when i am using the variable outside the loop its using the value which i initialized to the variable before the loop.
I think i have to use global variable, but i dont know how to make & use global variable.
counter=1
file=""
ls -1tr abc* | while read record
do
if [ counter -ne 0 ]
file=${record}
counter=0
fi
done
rfile=${file}
/* EOF */
now my rfile is empty it has no value, but if i echo file variable in while it is having some value, i want to use that value outside. Please suggest me a solution asap