Skip to main content
InterviewHubPRO
Section Overview
Q37

Question 37 of 50

Part 3: Advanced Text Processing & Production Internals (Senior / Lead)

74%
Bash & Shell Scripting (50 Qs)•Part 3: Advanced Text Processing & Production Internals (Senior / Lead)

Associative Arrays (Hash Maps) vs Indexed Arrays in Bash: Declaration, iteration, and pitfalls.

senior LevelMost AskedCode ExampleData Structures

In Simple Words: Bash 4.0+ supports key-value hash maps via declare -A:

bash
# Declare Associative Array (must use -A!)
declare -A HTTP_STATUS=(
  [200]="OK"
  [404]="Not Found"
  [500]="Internal Server Error"
)

# Insert or update
HTTP_STATUS[401]="Unauthorized"

# Access value
echo "${HTTP_STATUS[404]}" # Not Found

# Iterate over keys (!) and values
for code in "${!HTTP_STATUS[@]}"; do
  echo "HTTP $code -> ${HTTP_STATUS[$code]}"
done

# Array length
echo "Total codes: ${#HTTP_STATUS[@]}"