Bash arrays are one of those features that look intimidating until you use them once. After that, you’ll find yourself reaching for them constantly when writing scripts that handle lists of values.
What Is a Bash Array?
A bash array is a variable that holds multiple values. Instead of declaring separate variables for each item in a list, you put them in an array and reference them by index or key. Bash doesn’t care whether elements are strings or numbers, and it doesn’t enforce consistent types.
Indexed vs Associative Arrays
Indexed arrays use integers as keys, starting from 0. They’re the default and behave like arrays in most other languages. Associative arrays use strings as keys, giving you key-value pair functionality similar to hash maps or dictionaries. Associative arrays require explicit declaration with declare -A.
When to Use Arrays in Bash Scripts
Use arrays when you’re iterating over a list of servers, files, usernames, or any collection of items. Without arrays, you end up with multiple variables (server1, server2, server3) and conditional logic that duplicates code. With an array, one loop handles the entire list.
How to Declare a Bash Array
Declaring an Indexed Array
Three ways to declare an indexed array:
All at once:
fruits=("apple" "banana" "cherry")One by one:
fruits[0]="apple" fruits[1]="banana" fruits[2]="cherry"Using declare:
declare -a fruits=("apple" "banana" "cherry")Declaring an Associative Array
Associative arrays require the declare -A directive:
declare -A servers servers[web]="192.168.1.10" servers[db]="192.168.1.20" servers[cache]="192.168.1.30"Or inline:
declare -A servers=([web]="192.168.1.10" [db]="192.168.1.20")How to Add Variables to a Bash Array
For indexed arrays, append using the += operator:
fruits+=("date" "elderberry")Assign to a specific index:
fruits[5]="fig"Note: bash arrays are sparse. Assigning to index 5 when you only have 3 elements leaves indices 3 and 4 unset, not empty.
Referencing and Printing Array Elements
Access a Single Element
Use the curly brace syntax with the index:
echo ${fruits[0]}For associative arrays:
echo ${servers[web]}Print the Entire Array
The @ symbol expands all elements:
echo ${fruits[@]}To see all keys:
echo ${!fruits[@]}Get the Array Length
Prefix with # to get the bash array length:
echo ${#fruits[@]}For a specific element’s string length:
echo ${#fruits[0]}How to Remove Bash Array Elements
Use unset to remove a specific element:
unset fruits[1]This removes the element but leaves the index gap. The array length decreases by one. To remove the entire array:
unset fruitsHow to Loop Through a Bash Array
Loop with For-In
The standard way to iterate over array values:
for fruit in "${fruits[@]}"; do echo "$fruit" doneThe quotes around ${fruits[@]} handle elements with spaces correctly. Without them, an element like ‘passion fruit’ splits into two.
Loop with Index
When you need the index alongside the value:
for i in "${!fruits[@]}"; do echo "Index $i: ${fruits[$i]}" doneThe ! prefix expands the array’s indices rather than its values.
Passing a Bash Array to a Function
Bash doesn’t natively pass arrays to functions. The workaround: pass the array name and use indirect expansion inside the function:
function process_array { local -n arr=$1 for item in "${arr[@]}"; do echo "Processing: $item" done } my_list=("file1" "file2" "file3") process_array my_listThe -n flag on local creates a name reference. The function operates on the original array without copying it.
FAQ: Bash Array
For an indexed array: fruits=(“apple” “banana” “cherry”) For an associative array: declare -A servers=([web]=”192.168.1.10″ [db]=”192.168.1.20″)
Use for-in: for item in “${array[@]}”; do echo “$item”; done. Always quote “${array[@]}” to handle elements containing spaces.
Use ${#array[@]}. Example: count=${#fruits[@]}. This gives the number of elements in the array.
An associative array uses string keys instead of integer indices. Declare it with declare -A: declare -A config=([host]=”localhost” [port]=”3306″). Access values with ${config[host]}.