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


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


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


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.

  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:

a=('a1=(n1 n2 n3)' 'a2=(m1 m2 m3)')
for i in "${a[@]}"; do eval "${i}"; done
x=0;y=2;echo "${a1[${x}]}${a2[${y}]}"

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

a=('a1=(n1 n2 n3)' 'a2=(m1 m2 m3)' 'a3=(o1 o2 o3)')
for i in "${a[@]}"; do eval "${i}"; done
x=0;y=2;z=1;echo "${a1[${x}]}${a2[${y}]}${a3[${z}]}"

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