Bash does not support multi-dimensional arrays, but there is a way to imitate this functionality, if you absolutely have to.

As a quick example, here’s a data table representing a two-dimensional array.

  1 2 3
a1 n1 n2 n3
a2 m1 m2 m3

And here’s the graphical representation of this two-dimensional array with the values you would expect for each y[x] position:

  a2[0] a2[1] a2[2]
a1[0] n1m1 n1m2 n1m3
a1[1] n2m1 n2m2 n2m3
a1[2] n3m1 n3m2 n3m3

And here’s how we do it:

What about a three-dimensional array? Not gonna draw you a cubical table, but here’s the code:

This may seem a bit awkward and laborious, compared to the proper programming languages, but this can be extremely useful.

Think about it: a three-dimensional array holding data like timestamps, CPU I/O wait time, and network bandwidth utilization. This would be perfect for analyzing a CPU bottleneck that you suspect has something to do with time of day and network activity.

Leave A Reply

Please enter your comment!
Please enter your name here