3

I've 3 dates(could be more) taking as arguments as:

DATE_0="01-JAN-11"
DATE_1="01-JAN-12"
DATE_2="01-JAN-13"

Now I want to loop on dates and want to append values after _ by another variable.

for (( i=0; i<3; i++ ))
do
    echo $DATE_$i
done

I want when i=0 when it should print 01-JAN-11, when i=1 then print 01-JAN-12 and so on. Can anyone help me??

1 Answer 1

3

You can use indirect parameter expansion

for ((i=0; i<3; i++)); do
    var=DATE_$i
    echo ${!var}
done

but an array is a better solution

DATES=(01-JAN-11 01-JAN-12 01-JAN-13)
for d in "${DATES[@]}"; do
   echo $d
done

or

for ((i=0; i<${#DATES[@]}; i++)); do
   echo ${DATES[i]}
done
Sign up to request clarification or add additional context in comments.

1 Comment

for ((i=0; i<3; i++)); do var=DATE_$i echo ${!var} done This worked for me :) thanks chepner.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.