You may find very useful to use the -x option of bash for debugging: if you'd run the script as in
you would be aware that it doesn't work in the way you expected (neither with numbers nor with strings). To assign variables (without the
let statement) you can't use other variables to build the name in the left-hand side of the assignment, otherwise the entire line is interpreted as a command. If you really want to build the variable name using the value of any other variable, you have to consider the
eval statement, as you have done for the "print section". For example:
Code:
my_var=hello
eval ${my_var}_world=75 # hello_world=75
echo $hello_world # 75
Then be careful when you put together (concatenate) variables and strings: the statement
Code:
let ARRAY_$MONTH_$DAY=$DAY
has the string "ARRAY_", the value of a variable $MONTH_ (undefined) and the value of the variable $DAY. In this case the syntax $MONTH cannot be used: the most correct syntax ${MONTH} avoids errors. In summary, a correct version of the script could be
Code:
for MONTH in jan feb mar
do
for DAY in 1 2 3
do
eval ARRAY_${MONTH}_${DAY}=${DAY}
eval echo \$ARRAY_${MONTH}_${DAY}
eval ARRAY_${MONTH}_${DAY}="test"
eval echo \$ARRAY_${MONTH}_${DAY}
done
done
where the brackets in red are mandatory! Also, when you have the basics clear, you may consider to use arrays in bash... without trying to emulate them!
