0

I'm currently learning script shell programming(bash), and trying to make multiple arrays with different names by looping so that I could put the data according to the length of my data files. I've done getting the length of the data file(marked as 'num'), and here's what I could think in my brain.

for (( i=0; i<num; i++ ))
do  
    let array$i=()
done

I've looked up many method but it didn't work. I'm a newbie to bash so any comments / recommendations / teachings would be so appreciated. Thank you.

6
  • 1
    What you are looking for is probably namerefs (with a recent enough version of bash), or indirection. Just type man bash and then search for one or the other. Note: let is not what you apparently think. In bash it introduces arithmetic expressions. Commented Feb 15, 2022 at 12:32
  • @RenaudPacalet : How can a nameref be used to dynamically create a set of arrays? I only know how we can use it to refer to an existing variable, i.e. we have manually created a number of arrays and use a nameref to dynamically refer to one of them. The OPs problem seems to be to actually create the arrays, without explicitly naming them. Of course, an eval arr$i=(....) would work, but is ugly and error-prone. Commented Feb 15, 2022 at 13:43
  • Does this answer your question? Dynamic variable names in Bash Commented Feb 15, 2022 at 13:47
  • 1
    @user1934428 Easy. Always use the same nameref but let it point to different variable names: declare -n nr=array1, then do whatever you want with variable nr, it will be the same as if you do it with variable array1. Example: nr=([foo]=1 [bar]=2); printf '%s\t%s\n' "${!array1[@]}" "${array1[@]}" will print foo\tbar\n1\t2\n. Give it a try. Next you can declare -n nr=array2 and start working with your second array. And of course you can declare -n nr="array$i" if you wish. Commented Feb 15, 2022 at 14:25

1 Answer 1

2

With a recent enough bash, using namerefs:

for (( i=0; i<num; i++ )); do  
  declare -n nr="array$i"
  declare -a nr=()
done

And then, each time you want to add elements to your array10:

i=10
declare -n nr="array$i"
nr+=( foo bar )

To access the elements you can use the nameref (nr) or the array name, it works the same (as long as you do not modify the nameref attribute):

$ i=10
$ declare -n nr="array$i"
$ printf '%s\n' "${nr[@]}"
foo
bar
$ printf '%s\n' "${array10[@]}"
foo
bar

Think of it as a kind of reference.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.