Create and access shell variable having a name created by another string

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: , , ,

21 Responses to “Create and access shell variable having a name created by another string”

  1. Priyanka Says:

    yup!! it saved a lot of time for me! Thanks🙂

  2. Jonathan Lumb Says:

    Thank you soooo much… this is just what I was looking for

  3. suppie Says:

    thanks so much.. you saved my life

  4. Nagilum Says:

    “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).”

  5. Jaume Says:

    Thanks so much for this, it saved a lot of time.

  6. roemer2201 Says:

    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?

  7. Dumps Says:

    $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.

  8. THANKKKSSS Says:

    THANKSSSSSSSS

  9. Aritz TG (@AritzTG) Says:

    Thanks! This saved a lot of my time!

  10. Metafaniel Mexique Says:

    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}

  11. pirogoeth Says:

    I love you. This just saved me so much time and made a bigger project possible for me🙂

  12. biomonika Says:

    Thanks a lot for article and all the comments, especially Metafaniel’s🙂

  13. Swapnil Says:

    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

  14. Rincewind Says:

    Helped me a lot. Thanks Saminda.

  15. sobti Says:

    Doesn’t work for me. I am using sh.
    It gives me the error “Bad substitution”

  16. DF13 Says:

    Works great, thanks!

    To sobti, I think you gotta use bash.

  17. Chaitanya Says:

    Thank you soooooooooo much.. Helped me a lot..

  18. 백재연 Says:

    good job, thanks

  19. Juan Says:

    Yeahhhh! this saved time indeed,

  20. shennanigan83 Says:

    Awesome!!!! This made it so clear for me to understand. Saved me a ton of time! Thank you sooooooo much!

  21. Laks Says:

    Awesome…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: