Variables

I need to define a variable of variable. I'll try to explain it.

I've a list:

LIST="aaa bbb ccc"

I need to do something like:

for word in LIST ;do

  res_$word=`ls $word`

done

This doesn't work. Any idea?
Thanks

You need two changes:

for word in $LIST ;do

  eval res_$word=`ls $word`

done

You need to put a $ in front of the LIST in line 1 and you need to eval the assignment in line two.

# ./script
FUll list "aaa bbb ccc"
 aaa is listinggg now orderly aaa
FUll list "aaa bbb ccc"
 bbb is listinggg now orderly bbb
FUll list "aaa bbb ccc"
 ccc is listinggg now orderly ccc
# cat script
#!/bin/bash
LIST="aaa bbb ccc"
IFS=" "
for word in $LIST ; do
     eval res_$word=`ls -1 $word`
     echo -e "FUll list \"$LIST\"\n $res_$word is listinggg now orderly" `echo $res_$word`
   done

Ok. I write the real problem. My script is:

CENTROS="asi_ bkg_ gfz_ gop_ ige_ knmi lpt_ meto ngaa rob_ sgn_"

for centro in $CENTROS;do
	 estac_$centro=`awk '{print $1}' $dir_listas/$centro.list | cut -c 1-4`
done

if I write:

CENTROS="asi_ bkg_ gfz_ gop_ ige_ knmi lpt_ meto ngaa rob_ sgn_"
for centro in $CENTROS;do
	eval estac_$centro=`awk '{print $1}' $dir_listas/$centro.list | cut -c 1-4`
	
	echo $estac_$centro

done

The output is something like:

....
script.ksh[27]: eval[20]: LEIJ: not found [No such file or directory]
script.ksh[27]: eval[21]: MUEJ: not found [No such file or directory]
....

when LEIJ and MUEJ is the variable that should be worth.