# Variable within variable expansion

**URL:** <https://community.unix.com/t/variable-within-variable-expansion/377327>\
**Category:** Shell Programming and Scripting\
**Created:** [March 2, 2020, 4:15pm UTC](https://community.unix.com/t/variable-within-variable-expansion/377327 "2020-03-02T16:15:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hburnswell](https://community.unix.com/letter_avatar/hburnswell/32/6_449c533f586939b6e5f246632a46421f.png) [@hburnswell](https://community.unix.com/u/hburnswell)\
**Post date:** [March 2, 2020, 4:15pm UTC](https://community.unix.com/t/variable-within-variable-expansion/377327/1 "2020-03-02T16:15:37Z")

</div>

Hi,

I have been trying to get variable within variable expansion to work but have not been able to succeed. It seems that the two suggestions I am seeing are using 'eval' and using the built in bash capability of "${!test}". Here is a snippet of what I"m trying to accomplish:

```nohighlight
#!/bin/bash
b2b_qa_site=1234
b2c_qa_site=4321
b2b_prd_site=5678
b2c_prd_site=8765

grp=`echo "${1: -2:1}"`
env=`echo "${1: -3:1}"`

if [[$env -eq 4]]; then

        env=prd

else

        env=qa

fi

if [[$grp -eq 0]]; then

        grp=b2b

else

        grp=b2c

fi

test=${grp}_${env}_site
eval echo \$test

```

This would be used as:

```nohighlight
# ./script box400

```

And ideally receive:

```nohighlight
5678

```

Can anyone shed some light on what I am doing wrong here? I appreciate any guidance.

Thanks,  
HB

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 2, 2020, 4:51pm UTC](https://community.unix.com/t/variable-within-variable-expansion/377327/2 "2020-03-02T16:51:58Z")

</div>

You were close. Try either

```nohighlight
eval echo \$$test
5678

```

or

```nohighlight
echo ${!test}
5678

```

Your approach might not be the best one. Ever thought about e.g. (associative) arrays?

---

<div class="post-metadata">

**Author:** ![hburnswell](https://community.unix.com/letter_avatar/hburnswell/32/6_449c533f586939b6e5f246632a46421f.png) [@hburnswell](https://community.unix.com/u/hburnswell)\
**Post date:** [March 2, 2020, 5:30pm UTC](https://community.unix.com/t/variable-within-variable-expansion/377327/3 "2020-03-02T17:30:43Z")

</div>

@RudiC, much appreciated.

Your comment on this not being the best approach is not surprising ;-)... I continue to try and improve my efforts but due to my skill level, usually start with functionality. I will investigate your suggestion of associative arrays.

Any high level example of how to better do what I'm trying with associative arrays?

Thanks again..

HB

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [August 19, 2020, 8:46pm UTC](https://community.unix.com/t/variable-within-variable-expansion/377327/4 "2020-08-19T20:46:28Z")

</div>

Automatically bumped on August 19, 2020, 8:46pm UTC


