Reputation: 445
Good morning, i need to dynamically change the name of a variable. I did some tests. my test script is this:
i=5
range5min=777
varname="range${i}min"
echo ${!varname}
#$`echo $varname`=555
$varname=555
echo $range5min
I can access its content with:
echo $ {! Varname}
But I can't set a dynamically created variable with a value, that is, I can't make the assignment of a value with the equal. I tried both of these ways:
$ `echo $ varname` = 555
$ varname = 555
What I would like to do is create new variables inside a for, depending on how many loops the for expects.
Upvotes: 3
Views: 61
Reputation: 46823
you can use printf
:
printf -v "$varname" '%s' "555"
Note, though, that this type of programming is not really recommended, since it makes code hard to read and understand.
An array or associative array might be more appropriate.
Upvotes: 3