Variable variables

Hello,

Can you please help here?

DAY=$1

MONTH_MONDAY_YEAR = 1 2 3 4



	for i in ${MONTH_${DAY}_YEAR}
	do
		echo ${i}
	done

./test.sh MONDAY
./test.sh: line 3: MONTH_MONDAY_YEAR: command not found
./test.sh: line 10: ${MONTH_${DAY}_YEAR}: bad substitution

Hi, there are some errors
1) assegments without spaces

MONTH_MONDAY_YEAR=1 2 3 4

2) this sintax is not possible

${MONTH_${DAY}_YEAR}

But the question is: what is the expected result?

Like to know is there an alternative for

${MONTH_${DAY}_YEAR}

if it is not possible.

Ideally I like to use the value of MONTH_MONDAY_YEAR in the for loop

for i in ${MONTH_${DAY}_YEAR}

Check this out

LINK

Thanks to PikK45: then I suggest this code

DAY=$1
MONTH_MONDAY_YEAR="1 2 3 4"
eval echo \$MONTH_${DAY}_YEAR |tr -s ' ' '\n'| while read i
do
echo ${i}
done

A modern shell will let you dereference a string with ${!...}

You must assign the string first, you can't embed the string inside the {}

STR="${MONTH}_${DAY}_${YEAR}"

echo "${!STR}"
1 Like

@Corona688 it's fantastic:

DAY=$1
MONTH_MONDAY_YEAR="1 2 3 4"
STR="MONTH_${DAY}_YEAR"
for i in ${!STR}
do
echo ${i}
done