Global variable

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.

Plz respond asap

prefix the variable you want to use with export.
i think this should work. give a try. Also tell which shell you are using.

Thanks
namish

Please define type of script: awk, sed, bash, ksh.

In bash all variables are global unless you declare them local to a function for example.

Show us your code.

i have used export but this is not working, may be i am not using correctly
can u plz give me a example of that
:wink:

Before anybody can really help you, you need to tell the forum what particular shell you are using.

i m using bash
and my code is like this

/* Code starts here */

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