Skip to content
koalaman edited this page Aug 7, 2016 · 1 revision

Bash does not support multidimensional arrays. Use 1D or associative arrays.

Problematic code:

foo[1][2]=bar
echo "${foo[1][2]}"

Correct code:

In bash4, consider using associative arrays:

declare -A foo
foo[1,2]=bar
echo "${foo[1,2]}"

Otherwise, do your own index arithmetic:

size=10
foo[1*size+2]=bar
echo "${foo[1*size+2]}"

Rationale:

Bash does not support multidimensional arrays. Rewrite it to use 1D arrays. Associative arrays map arbitrary strings to values, and are therefore useful since you can construct keys like "1,2,3" or "val1;val2;val3" to index them.

Exceptions:

None.

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally