Cybersecurity Updates & Tools

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.