🐚 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 -A first |
| 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 :
