๐Ÿงฉ 2. Bash Basics & Syntax
Estimated reading: 3 minutes 44 views

๐Ÿš 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

MistakeFix
Accessing undeclared associative arrayUse declare -A first
Using unquoted variables in loopsAlways quote "${array[@]}"
Trying associative arrays in Bash < 4Upgrade 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 arr for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐ŸŸข Bash: Arrays & Associative Arrays (arr=(), declare -A)

Or Copy Link

CONTENTS
Scroll to Top