exporting variable

Hi All;

I m working on a script and came across an issue ,to explain it briefly here is the sample code

echo $HOSTNAME|while read IN; do var=`echo $IN|awk -F "-" '{print $2}'`; export var; echo $var; done

now I get the value of $var but when it is out of the while loop
it does not return me a value.

I tried using typeset,still the same issue

So the question is when i set a value for a variable and export it in a while loop the value of the variable is not returned after I echo it out of the loop.

Thanks in advance
Syed

P.S : It can be done a one-liner; but my requirement it to run it through an array so I need the while loop.

Please state the Operating System and which Shell. As this is behaving like an old Bourne shell, do you have a Posix shell or ksh available?

Sorry ,I initially forgot to add details.Im using RHEL 5 and shell is bash

---------- Post updated at 01:16 AM ---------- Previous update was at 01:13 AM ----------

Finally it works,I have fixed it

for IN in $HOSTNAME; do var=`echo $IN|awk -F "-" '{print  $2}'`;  export var; echo $var; done

An "export" statement only works in the current shell or subshell and makes a variable available to any scripts you call. It does not make the variable available to the parent shell. In this case the "export" statement is surplus in both examples. I can't explain in this context why "while" created a subshell when "for" did not.
cfajohnson may know better.