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.
evalby replacing the eval line withX=${!i}which will assign toXthe value of the variable whose name is stored ini${}is a general syntax for accessing a variable (though it can often be shortened to $varname). You can do many things within the{}to affect the variable access, one of which is if the first character in{}is!the rest is another variable name (iin this example) then the value in that other variable (i) will be expanded and treated as the name of the variable to access with the${}, so ifi=VAR1then${!i}becomes${VAR1}which is the same as$VAR1