Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 1
    You can do the same thing awk does: emulate multi-dim arrays with single-dim associative arrays. Eg. declare -A a; i=2; j=3; a[$i,$j]=4; echo ${a[2,3]}. Of course, nicely iterating through rows and cols will be a bitch. But if you need to handle complex data structures, using bash is certainly not the best idea. Commented Oct 21, 2020 at 21:31
  • 1
    Or emulate them with indexed arrays: declare -a a; i=2; j=3; a[i<<16|j]=4; echo ${a[2<<16|3]} (assuming that 65536 x 65536 is enough for everybody ;-)). Commented Oct 21, 2020 at 21:39
  • 1
    In case it wasn't clear, the indexed arrays in bash are sparse; a[1<<30]=foo will NOT allocate 1G of empty slots. And like everything in bash, they're also very slow ;-) Commented Oct 21, 2020 at 22:00
  • 3
    The shell is not the type of language you usually want to use for things requiring multi-dimensional arrays. It may be more efficient to move to some other scripting language, such as Python, Perl, or any common compiled language. Commented Oct 22, 2020 at 6:17
  • 2
    @JeremyBoden If speed is not an issue, then why not just emulate a multi-dimensional array in a 1-dimensional array with a(i,j) == a[i*ncols + j] or similar transformation? This is a common way to do it when managing 1D structures is easier (or more efficient) than managing multi-D structures. Commented Oct 23, 2020 at 13:12