This took sometime to find. Apparently this was not something possible with any direct way. I wanted to have 2 dimentional array but in the end i decided to use dynamic strings to create variables which would simulate the 2d array.
Say i want n variables like “test1,test2,test3 …… testn” where n is decided at the time the script it running. So what I should do to create the variable is pretty simple. I will show you how to create the nth variable.
say,
$ n=123
$ let test$n=$n*2
at this point there is a variable now ‘test123’ and it have the value 246. to get the value is a little tricky. This is what took me a lot of time to find. In the end i found the solution in http://www.usenet-forums.com/linux-general/78879-shell-script-variable-variable-name.html.
First you have to create a variable which contains our dynamic variable name.
$ varName=test$n
Now the variable “varName” contains our dynamic variable name. If you print the value of this variable
$ echo ${varName}
test123
but if you specify “!” mark infront of our variable then the value denoted by the variable whos name is the value of “varName” is shown.
$ echo ${!varName}
246
well thats it actually. Hope this saved some time for someone.
Tags: bash create variable from string, dynamic string to create variables in bash script, dynamically create variables in bash script, new variable using custom string
July 1, 2009 at 10:46 am |
yup!! it saved a lot of time for me! Thanks 🙂
July 11, 2009 at 6:06 am |
Thank you soooo much… this is just what I was looking for
May 27, 2010 at 10:34 am |
thanks so much.. you saved my life
August 3, 2010 at 7:46 pm |
“eval” works also with sh which I prefer to use for scripts as it’s available almost everywhere.
n=123
eval “let test$n=$n*2;export test$n”
or to get the value:
echo “The value of test$n is $(eval echo \$test$n).”
January 27, 2011 at 9:48 am |
Thanks so much for this, it saved a lot of time.
February 5, 2011 at 6:55 pm |
Well, it seems that this doesn’t work, if your value is string with spaces I tried many combinations of “”. Anyone got a hint for me?
April 1, 2011 at 9:03 am |
$n = 2
eval test$n=’Hello World’
echo $test2
This will work for strings ! Don’t go for let, it is only for arithmetic operations.
July 18, 2011 at 10:18 am |
THANKSSSSSSSS
October 19, 2011 at 3:12 pm |
Thanks! This saved a lot of my time!
February 10, 2012 at 4:49 pm |
Quite useful, thanks a lot! Also @Dumps comment was very useful too, just don’t copy paste it as it is, remove the spaces between n and 2 😉 like in:
n=’Test’
eval var$n=’content’
varName=test$n
echo ${varName}
echo ${!varName}
June 2, 2012 at 2:12 pm |
I love you. This just saved me so much time and made a bigger project possible for me 🙂
November 22, 2012 at 11:53 am |
Thanks a lot for article and all the comments, especially Metafaniel’s 🙂
December 20, 2012 at 1:19 pm |
Thanks a Lot Saminda .. I spend around 6 hrs for expected solution .. finally got the expected solution after reading your blog…
Thanks a lot for Sharing such valuable information…
Thanks
Swapnil
October 22, 2013 at 8:07 pm |
Helped me a lot. Thanks Saminda.
November 29, 2013 at 9:17 am |
Doesn’t work for me. I am using sh.
It gives me the error “Bad substitution”
February 11, 2014 at 2:00 am |
Works great, thanks!
To sobti, I think you gotta use bash.
June 13, 2014 at 6:47 am |
Thank you soooooooooo much.. Helped me a lot..
July 2, 2014 at 5:20 am |
good job, thanks
September 22, 2014 at 6:52 pm |
Yeahhhh! this saved time indeed,
May 14, 2015 at 4:10 am |
Awesome!!!! This made it so clear for me to understand. Saved me a ton of time! Thank you sooooooo much!
October 8, 2016 at 9:43 am |
Awesome…