I want to write a script to reference multiple arrays from another array which holds these array's variable names.
Here's my code so far:
#!/bin/bash
array1=('array1string1' 'array1string2')
array2=('array2string1' 'array2string2')
array_names=('array1' 'array2')
for a in ${array_names[@]}
do
for b in ${a[@]}
do
echo $b
done
done
I'd like the output to scan through both arrays (from the outer for loop) and print the respective strings in the inner for loop which calls echo. My current output is just showing me:
array1
array2
I'd be grateful for any pointers regarding this. Thank you!
for b in "${array1[@]}" "${array2[@]}"; do ...; done
? – Kusalananda Sep 06 '17 at 17:14${a[@]}
to${!a}
does what you want (I think). – parkamark Sep 06 '17 at 17:24${!a[@]}
gives a length of the arraya
. – Kusalananda Sep 06 '17 at 17:27${!a}
is just giving me the first elements. – chnppp Sep 06 '17 at 17:29