Atomiklan
Atomiklan

Reputation: 5434

Bash array with variable

Any idea how I can declare a bash array with a variable embedded?

For example, i is a integer that increments in a for loop. I want to continue to increment i and append it to the end of the array being declared like so:

declare -a DB$iFIELDS
DB$iFIELDS[$j]=blah blah blah

Upvotes: 0

Views: 182

Answers (1)

chepner
chepner

Reputation: 530920

You can use declare to make the assignment as well, since to some extent the [] is as much a part of a variable name as it is an indexing operator.

$ i=3
$ declare -a DB${i}FIELDS
# ...
$ j=6
$ declare "DB${i}FIELDS[$j]=blah blah blah"
$ set | grep "DB.*FIELDS"
DB3FIELDS=([6]="blah blah blah")

Upvotes: 1

Related Questions