๐ Bash Arrays & Associative Arrays โ arr=(), declare -A with Examples
๐งฒ Introduction to Bash Arrays โ Indexed and Associative Arrays Explained
In Bash scripting, arrays let you store multiple values in a single variable, making your scripts more efficient and organized. Bash supports two types of arrays:
- Indexed arrays โ accessed by numeric indexes
- Associative arrays โ accessed using string keys
While indexed arrays are available by default, associative arrays require explicit declaration using declare -A.
๐ฏ In this article, youโll learn:
- How to declare, access, and modify indexed arrays
- How to use associative arrays with string keys
- Syntax differences between the two array types
- Real-world examples and best practices
๐ฆ What Are Arrays in Bash?
Arrays in Bash allow you to group related data together. Instead of using separate variables like val1, val2, val3, you can group them as:
arr=(val1 val2 val3)
๐ Indexed Arrays โ Syntax and Usage
โ Declare an Indexed Array
fruits=("apple" "banana" "cherry")
๐งพ Access Elements
echo ${fruits[0]}  # Output: apple
echo ${fruits[1]}  # Output: banana
๐ ๏ธ Add or Update Elements
fruits[3]="orange"
fruits[1]="mango"
๐ Loop Through Array
for fruit in "${fruits[@]}"; do
  echo "$fruit"
done
๐ Get Array Length
echo ${#fruits[@]}  # Number of elements
๐ง Associative Arrays โ Key-Value Storage
โ Declare an Associative Array
declare -A capitals
๐งพ Add Elements
capitals[India]="New Delhi"
capitals[France]="Paris"
capitals[Japan]="Tokyo"
๐ Access Elements
echo ${capitals[India]}  # Output: New Delhi
๐ Loop Through Keys
for country in "${!capitals[@]}"; do
  echo "$country: ${capitals[$country]}"
done
๐ Get All Keys or Values
echo "${!capitals[@]}"  # Keys
echo "${capitals[@]}"   # Values
โ ๏ธ Associative arrays require Bash version 4.0+ and must be declared using
declare -A.
๐งช Example: Student Grades (Associative Array)
declare -A grades
grades[Alice]=95
grades[Bob]=88
grades[Charlie]=78
for student in "${!grades[@]}"; do
  echo "$student scored ${grades[$student]}"
done
โ Output:
Alice scored 95
Bob scored 88
Charlie scored 78
๐ซ Common Mistakes to Avoid
| Mistake | Fix | 
|---|---|
| Accessing undeclared associative array | Use declare -Afirst | 
| Using unquoted variables in loops | Always quote "${array[@]}" | 
| Trying associative arrays in Bash < 4 | Upgrade or avoid declare -A | 
๐ Summary โ Bash Arrays & Associative Arrays
Arrays bring structure and efficiency to Bash scripts. Use indexed arrays for ordered lists, and associative arrays when you need key-value mappings. Mastering both allows you to build advanced CLI tools and automate complex logic.
๐ Key Takeaways:
- Use arr=(...)for indexed arrays
- Use declare -A arrfor associative arrays
- Use "${arr[@]}"for safe iteration
- Associative arrays require Bash 4+
โ๏ธ Real-world Uses:
- Manage user-role pairs
- Store filename-extension mappings
- Track counts, totals, or states in scripts
โ FAQ โ Bash Arrays & Associative Arrays
โ How do I check the length of a Bash array?
โ
 Use:
echo ${#array[@]}
This returns the number of elements.
โ Can Bash arrays be multi-dimensional?
โ
 No native support, but you can simulate with naming conventions or indexed keys:
matrix[0,0]=1
โ Are associative arrays supported in all Bash versions?
โ
 No. Associative arrays are supported only in Bash 4.0 or newer.
โ How do I loop through both keys and values in associative arrays?
โ
 Use:
for key in "${!arr[@]}"; do
  echo "$key: ${arr[$key]}"
done
โ What’s the safest way to loop through an array with spaces?
โ
 Always quote:
for val in "${array[@]}"; do echo "$val"; done
Share Now :
