Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Multi-Dimensional Arrays in Bash

Submitted by on March 30, 2019 – 4:06 pm

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.

[su_table]
  1 2 3
a1 n1 n2 n3
a2 m1 m2 m3
[/su_table]

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

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

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.

Print Friendly, PDF & Email

Leave a Reply