What does eval X=\$$i mean in UNIX

evalscriptingsolaris

I have a small script with the following lines

echo mom,dad |awk -F, '{print $1,$2}' | while read VAR1 VAR2
do
 for i in VAR1 VAR2
  do
   eval X=\$$i
   echo $X
 done  
done

OUTPUT:

mom
dad

What is this line doing eval X=\$$i?

I understand the rest of the lines, but I don't understand the iterations of this for loop with eval. Can someone shed light on this ? I am using Solaris 5.10 with Korn Shell.

Best Answer

eval performs an extra level of substitution and processing on the remainder of the line.

In the first iteration of the loop, i is set to "VAR1", and one level of backslash-escaping is reduced, so:

eval X=\$$i

becomes:

X=$VAR1

which evaluates to:

X=mom

(repeat for the next loop, only $i is then VAR2, and $VAR2=dad)

Related Question