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.
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.
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
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.
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]}" 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 #!/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.
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.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…