Search code examples
arraysbashlistecho

bash script to Echo multiple lists


i need to echo multiple list like this

list_0=(1 2 3 4 5 6)
list_1=(7 8 9)
list_2=(22 7 34 88)
.....
.....
list_99=(23 67 80)

I have this version that works but every time I have to declare new list in for comand

for nums in ${list_0[@]} ${list_1[@]} ${list_2[@]} ......... ${list_99[@]}
do
echo $nums
done

Is there any possibility to make it easier to go through the lists ?

something like this

x=0
for nums in ${list_(x until x=99)[@]}
do
echo $nums
done

Thank you


Solution

  • To replicate your desired double nested loop using variable dereference:

    for i in {0..99}; do v="list_$i[@]"
      for j in "${!v}"; do
        echo "$j"
      done
    done
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    22
    7
    34
    88
    :
    :
    23
    67
    80