Multi-dimensional arrays in Bash Multi-dimensional arrays in Bash arrays arrays

Multi-dimensional arrays in Bash


Bash does not support multidimensional arrays, nor hashes, and it seems that you want a hash that values are arrays. This solution is not very beautiful, a solution with an xml file should be better :

array=('d1=(v1 v2 v3)' 'd2=(v1 v2 v3)')for elt in "${array[@]}";do eval $elt;doneecho "d1 ${#d1[@]} ${d1[@]}"echo "d2 ${#d2[@]} ${d2[@]}"

EDIT: this answer is quite old, since since bash 4 supports hash tables, see also this answer for a solution without eval.


Bash doesn't have multi-dimensional array. But you can simulate a somewhat similar effect with associative arrays. The following is an example of associative array pretending to be used as multi-dimensional array:

declare -A arrarr[0,0]=0arr[0,1]=1arr[1,0]=2arr[1,1]=3echo "${arr[0,0]} ${arr[0,1]}" # will print 0 1

If you don't declare the array as associative (with -A), the above won't work. For example, if you omit the declare -A arr line, the echo will print 2 3 instead of 0 1, because 0,0, 1,0 and such will be taken as arithmetic expression and evaluated to 0 (the value to the right of the comma operator).


This works thanks to 1. "indirect expansion" with ! which adds one layer of indirection, and 2. "substring expansion" which behaves differently with arrays and can be used to "slice" them as described https://stackoverflow.com/a/1336245/317623

# Define each array and then add it to the main oneSUB_0=("name0" "value 0")SUB_1=("name1" "value;1")MAIN_ARRAY=(  SUB_0[@]  SUB_1[@])# Loop and print it.  Using offset and length to extract valuesCOUNT=${#MAIN_ARRAY[@]}for ((i=0; i<$COUNT; i++))do  NAME=${!MAIN_ARRAY[i]:0:1}  VALUE=${!MAIN_ARRAY[i]:1:1}  echo "NAME ${NAME}"  echo "VALUE ${VALUE}"done

It's based off of this answer here