0

This question already has an answer here:

I want to execute a for loop, create new variables with values that vary with the loop iteration, and refer to the new variables within the loop or later in the shell script. Can't figure out how to refer to the new variables because of the way they are being evaluated. A simple example of what I need but does not work -

for i in {1..4}
 do
 my${i}var="./path_to/my${i}file"
 echo $my${i}var
 done

marked as duplicate by don_crissti, Sergiy Kolodyazhnyy, Jeff Schaller, Kusalananda, roaima Feb 4 '17 at 22:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You can do this I suppose, but why not use an array variable instead? Assign with myvar[$i]=... and use with "${myvar[$i]}". – roaima Feb 4 '17 at 22:39
0

You want to use Bash arrays for that.

for i in 0 1 2 3
do
    myvar[$i]="./path_to/my${i}file"
    echo ${myvar[$i]}
done

Not the answer you're looking for? Browse other questions tagged or ask your own question.