Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why my bash multidimensional array code is not working?

i am trying to make a multidimaensional array in bash and access the elements. but its not working.

I tried with this code but there’s error

# Create an indexed array to hold the inner arrays
    array=()

# Initialize the inner arrays
  array[0]=(1 2 3)
  array[1]=(4 5 6)
  array[2]=(7 8 9)

# Access elements
  element_12="${array[1][2]}"
  echo "Element at [1][2]: $element_12"

# Loop through the elements
    for ((i = 0; i < ${#array[@]}; i++)); do
        inner_array=("${array[i][@]}")  # Copy the inner array
        for ((j = 0; j < ${#inner_array[@]}; j++)); do
            echo "Element at [$i][$j]: ${inner_array[j]}"
       done
   done

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

why my bash multidimensional array code is not working?

Because Bash does not support multidimensional arrays. In particular, Bash does not support assigning a list to an array member, which should be included in the error message:

$ array[0]=(1 2 3)                                                                                                                             
-bash: array[0]: cannot assign list to array member                                                                                              
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading