I am using bash version 5.1.16
I am trying to write a bash script that creates a dynamic number of arrays, each with a dynamic name that will hold some element parts from a declared array. I am attempting to split a single array into multiple arrays that will hold an equal number of elements.
dec_array = (abc10 def2 ghi333 jkl mno51 pqr_6) # this array will always have some multiple of three elements but variable in the number of elements
num_elem=3
num_arr=0 # will eventually incr and loop once this works
for ((i=0; i < ${#dec_array[@]}; i+=num_elem)); do
part=( "${dec_array[@]:i:num_elem}" )
echo "Elements in first example: ${part[*]}"
dyn_array_${num_arr}=( "${dec_array[@]:i:num_elem}" )
echo "Elements in second example: $dyn_array_${num_arr}[*]}"
done
output:
Elements in first example: abc10 def2 ghi333
Elements in first example: jkl mno51 pqr_6
syntax error near unexpected token `"${dec_array[@]:i:num_elem}"'
` dyn_array_${num_arr}=( "${dec_array[@]:i:num_elem}" )'
I need to be able to split dec_array into multiple dyn_array_n, e.g.,
dyn_array_0: abc10 def2 ghi333
dyn_array_1: jkl mno51 pqr_6
What is the correct syntax to accomplish creating a dynamic number of arrays with dynamic names?