How To

How To Use Associative Arrays In Bash

Introduction

Associative arrays in Bash are used to store data in key-value pairs. Unlike normal Bash arrays, where values are accessed using numbers like 0, 1, and 2, associative arrays use custom names as keys.

For example, you can store a username with a role, a tool name with its purpose, or a service name with its port number. If you are learning Bash scripting, associative arrays are useful for writing organized scripts, especially for automation, Linux administration, cybersecurity checks, and configuration management.

What Is An Associative Array In Bash?

An associative array stores values using named keys.

Example:

declare -A user_rolesuser_roles[admin]="Administrator"user_roles[guest]="Limited User"

Here, admin and guest are keys, while Administrator and Limited User are values.

Create An Associative Array In Bash

Before using an associative array, you must declare it with:

declare -A array_name

Create a script:

nano associative-array.sh

Add this code:

#!/bin/bashdeclare -A toolstools[nmap]="Network scanner"tools[curl]="HTTP request tool"tools[grep]="Text search tool"echo "Nmap is a ${tools[nmap]}"echo "Curl is a ${tools[curl]}"echo "Grep is a ${tools[grep]}"

Run the script:

chmod +x associative-array.sh./associative-array.sh

Output:

Nmap is a Network scannerCurl is a HTTP request toolGrep is a Text search tool

Loop Through Associative Array Keys

You can loop through all keys using ${!array[@]}.

#!/bin/bashdeclare -A portsports[ssh]=22ports[http]=80ports[https]=443for service in "${!ports[@]}"do    echo "$service runs on port ${ports[$service]}"done

This script prints each service name and its port number.

Add New Values To Associative Array

You can add new key-value pairs anytime.

#!/bin/bashdeclare -A usersusers[admin]="root access"users[developer]="code access"users[analyst]="log access"echo "Analyst permission: ${users[analyst]}"

Check If A Key Exists

You can check whether a key exists in an associative array.

#!/bin/bashdeclare -A servicesservices[ssh]=22services[http]=80if [[ -n "${services[ssh]}" ]]; then    echo "SSH service exists"else    echo "SSH service not found"fi

Cybersecurity Example: Service And Port Mapping

#!/bin/bashdeclare -A common_portscommon_ports[ssh]=22common_ports[ftp]=21common_ports[http]=80common_ports[https]=443for service in "${!common_ports[@]}"do    echo "Checking $service on port ${common_ports[$service]}"done

This is useful when creating simple security scripts that check common ports and services.

Conclusion

Associative arrays in Bash help store data using meaningful keys instead of numeric indexes. They are useful when working with services, ports, usernames, roles, tools, settings, and configuration values.

For beginners, learning associative arrays is helpful because they make Bash scripts cleaner and easier to understand. They are especially useful in Linux automation, cybersecurity scripting, service checking, and system administration tasks.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

3 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

3 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

3 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

3 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

3 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

4 days ago