Skip to content

Files

Latest commit

 

History

History
34 lines (24 loc) · 719 Bytes

SC2180.md

File metadata and controls

34 lines (24 loc) · 719 Bytes

Pattern: Use of multidimensional array in Bash

Issue: -

Description

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.

Example of incorrect code:

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

Example of 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]}"

Further Reading