A
A
Alexander Molokovich2017-05-27 10:57:08
bash
Alexander Molokovich, 2017-05-27 10:57:08

How to generate variables in bash in a loop?

Is variable name generation possible in bash?
Here is an example

for i in 01 02 03 04 05 06 07 08 15 30 45 60
do
d${i}="${i}day"
done

So that I would have output variables with values:
d01="01day"
d02="02day"
and so on

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Golf, 2017-05-27
@Golf

days=(01 02 03 04 05 06 07 08 15 30 45 60)

for i in ${days[*]}
do
    echo d${i}=\""${i}day\""
done

3
3vi1_0n3, 2017-05-28
@3vi1_0n3

for i in 01 02 03 04 05 06 07 08 15 30 45 60
do
  eval d${i}="${i}day"
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question