Associative Arrays for Key-Value Data
Associative Arrays for Key-Value Data
Associative arrays (hashes/maps) map arbitrary string keys to string values (Bash 4.0+):
# MUST be explicitly declared with -A
declare -A ports=(
["http"]="80"
["https"]="443"
["ssh"]="22"
)
# Adding or updating
ports["dns"]="53"
# Querying keys and values
echo "SSH port: ${ports["ssh"]}"
# Iterating over all keys
for service in "${!ports[@]}"; do
echo "Service $service runs on port ${ports[$service]}"
done