# Creating bash array name from variable

**URL:** <https://community.unix.com/t/creating-bash-array-name-from-variable/305422>\
**Category:** Shell Programming and Scripting\
**Created:** [February 27, 2012, 1:38am UTC](https://community.unix.com/t/creating-bash-array-name-from-variable/305422 "2012-02-27T01:38:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![melias](https://community.unix.com/letter_avatar/melias/32/6_449c533f586939b6e5f246632a46421f.png) [@melias](https://community.unix.com/u/melias)\
**Post date:** [February 27, 2012, 1:38am UTC](https://community.unix.com/t/creating-bash-array-name-from-variable/305422/1 "2012-02-27T01:38:45Z")

</div>

Hi gurus,

I need to create arrays from variables, via a loop.

The issue I have is with the array name creation. How do I use a variable to define an array?

I want to do something like  
declare -a $H  
where $H is my loop variable.

I then need to add items to each array I've created, something like this  
$H=( ${$H[@]} "$I")

$I is the variable I'm using to loop in the array items.

I'm guessing that I'll need to use eval but nothing I've tried works. Escaping non-printable characters doesn't work either.

I can define an empty array using the $H variable, but I'm unable to add items to it or print out the full array item list.  
I have no problems when defining a non-variable array name, but I need to create over 100 lookup tables, with number of items ranging from 1 to 10 -15 for each array, all from loop.

Any suggestions would be most welcolmed

---

<div class="post-metadata">

**Author:** ![guruprasadpr](https://community.unix.com/user_avatar/community.unix.com/guruprasadpr/32/1542_2.png) [@guruprasadpr](https://community.unix.com/u/guruprasadpr)\
**Post date:** [February 27, 2012, 1:48am UTC](https://community.unix.com/t/creating-bash-array-name-from-variable/305422/2 "2012-02-27T01:48:58Z")

</div>

```nohighlight
H[${#H[*]}]=$I

```

Guru.

---

<div class="post-metadata">

**Author:** ![melias](https://community.unix.com/letter_avatar/melias/32/6_449c533f586939b6e5f246632a46421f.png) [@melias](https://community.unix.com/u/melias)\
**Post date:** [February 27, 2012, 9:14pm UTC](https://community.unix.com/t/creating-bash-array-name-from-variable/305422/3 "2012-02-27T21:14:31Z")

</div>

Thanks Guru, but not sure if this is what I'm after.

example of my code is:

> [@](#):
>
> for H in "list of items"  
> do declare -ax $H Setting the array name  
> for I in "list of array items"  
> do "$H"=( "${$H[@]}" "$I" ) adding array items to required arrays  
> echo ${"$H"[@]} print all items in the array just added to  
> done  
> done

e.g. Array name (H) could contain a persons name. The items (I) in the array could include things like address, phone number, D.O.B.

So I need to use a for loop to create the initial arrays (persons name), then use another for loop to populate the arrays with specific items. Then I need to be able to print out specific items or the entire array.  
The issue looks to be with my variable expansion, but I could be wrong.

Does this make better sense?

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [February 27, 2012, 10:50pm UTC](https://community.unix.com/t/creating-bash-array-name-from-variable/305422/4 "2012-02-27T22:50:20Z")

</div>

Perhaps this might help:

```nohighlight
H=boats
eval $H'[1]="Testing"'
eval echo '${'$H'[1]}'
echo ${boats[1]}

```

Thing is, that eval ends up running a sub shell so it probably won't be real fast to assign 500 arrays of 10-15 items each.

Another thing you could try and do is write out a whole head of assign statements to a file and then source it into the current shell

```nohighlight
H=fish
echo $H'[1]="carp"' > /tmp/asgn_$$
echo $H'[2]="cod"' >> /tmp/asgn_$$
echo $H'[3]="marlin"' >> /tmp/asgn_$$
 
. /tmp/asgn_$$
rm -f /tmp/asgn_$$
 
echo ${fish[2]}

```
