Contrarily to ksh, that allows multidimensional arrays, bash (4.2 or below) does not allow multidimensional arrays. I work around this by having a separator in the elements and then each element of the first array can be made in to an array itself. Foe example:
GPIO=("in:down:0:22:1-1.4:17:usb:usb port 3 mgmt button"
"in:down:0:13:1-1.5:18:usb:usb port 4 mgmt button")
then to access the third element of the first row I'd do something like this:
eval $(echo "ROW0=($(echo "${GPIO[0]}" | awk -F: '{ for(i=1;i<=NF;i++) printf("\"%s\" ",$i);}' ))")
echo ${ROW0[2]}
I don't like doing it but I've not found a neater way around the problem. Is there a neater and more efficient way around the problem while still approaching it like an array ?
There is also a workaround using associative arrays and adding a fake second index in the element index:
declare -A FAKE2DIMARRAY
FAKE2DIMARRAY=(
[0,0]="first row first element"
[0,1]="first row second element"
[1,0]="second row first element"
[1,1]="second row second element"
)
Although this solution is really neat and efficient it makes the definition of the array a mess if it has many rows and elements. For instance my full GPIO array is actually 16x8 and that would require 128 lines and taking care to correctly index each line ... that's less then optimal too in this case.