Bash Arrays for Storing Lists in Shell Scripts
Bash arrays let you keep a list of values under one variable name and read, update, loop, or print those values by index. In Bash, array elements are not limited to one data type, so the same array can contain strings, numbers, paths, command names, or other text values.
The most common Bash array is an indexed array. Indexed arrays use numeric indexes, starting at 0. Bash also supports associative arrays, where string keys are used instead of numeric indexes. This tutorial focuses on indexed Bash arrays first, then introduces associative arrays and common mistakes to avoid.

Bash Array Syntax at a Glance
The following syntax shows the usual operations you need when working with a Bash array: create the array, read one element, print all elements, count elements, and read the assigned indexes.
array_name=("first" "second" "third")
echo "${array_name[0]}" # first element
echo "${array_name[@]}" # all elements
echo "${#array_name[@]}" # number of elements
echo "${!array_name[@]}" # assigned indexes
When a value contains spaces, quote it inside the array and also quote the array expansion while reading it. This prevents Bash from splitting one element into multiple words.
Bash Array Tutorials on TutorialKart
The following Bash array tutorials cover individual array operations with focused examples. Use them when you want a separate explanation for one task, such as finding the length, looping through values, appending elements, or slicing an array.
Bash array basics
- Bash – Initialize Array
- Bash – Length/Size of Array
- Bash – Access Elements(s) of Array using Index
- Bash – Print All Elements of Array in Single Line to Console
Looping through Bash arrays
- Bash – Loop through Array Elements
- Bash – Loop through Array Indices
- Bash Array – While Loop
- Bash Array – For Loop
Finding and checking Bash array elements
Updating, appending, and slicing Bash arrays
- Bash – Update Element of an Array at Specific Index
- Bash – Append Element(s) to Array
- Bash – Append an Array to another Array
- Bash – Slice an Array
Bash Indexed Array Declaration with declare -a
To declare a variable as an indexed Bash array, use the declare command with the -a option. Declaration is optional for many simple arrays because assigning with parentheses also creates an array, but declare -a makes the intent clear in longer scripts.
The syntax is:
declare -a arrayname
Example
In the following script, we declare an array with name fruits.
declare -a fruits
Bash Array Initialization with Parentheses
To initialize a Bash Array, use assignment operator =, and enclose all the elements inside braces (). The syntax to initialize a bash array is
arrayname=(element1 element2 element3)
There must be no spaces around the = sign. Write fruits=("apple" "banana"), not fruits = ("apple" "banana").
Example
In the following script, we initialize an array fruits with three elements.
fruits=("apple" "banana" "cherry")
If an array element contains whitespace, quote that element while initializing the array.
fruits=("red apple" "ripe banana" "sweet cherry")
numbers=(10 20 30)
mixed=("apple" 25 "cherry")
Access Bash Array Elements by Zero-Based Index
We can access elements of a Bash Array using the index. The first element is at index 0, the second element is at index 1, and so on.
echo ${ARRAY_NAME[2]}
In scripts, it is usually safer to quote the expansion, especially when the element may contain spaces.
echo "${ARRAY_NAME[2]}"
Example
In the following script, we access elements of array fruits with indices.
fruits=("apple" "banana" "cherry")
echo ${fruits[0]}
echo ${fruits[1]}
Output
apple
banana
Print Bash Array Values, Indexes, and Element Count
To print all elements of a Bash array, use "${arrayname[@]}". To print the assigned indexes, use "${!arrayname[@]}". To get the number of assigned elements, use "${#arrayname[@]}".
| Bash array expression | Meaning |
|---|---|
"${fruits[0]}" | Element at index 0 |
"${fruits[@]}" | All array elements, preserved as separate values when quoted |
"${#fruits[@]}" | Number of assigned elements |
"${!fruits[@]}" | All assigned indexes |
"${fruits[@]:1:2}" | Slice two elements, starting from index 1 |
To print all the elements of a bash array with all the index and details use declare with option p.
declare -p arrayname
Example
fruits=("apple" "banana" "cherry")
declare -p fruits
Output
declare -a fruits='([0]="apple" [1]="banana" [2]="cherry")'
Loop Through Bash Array Elements with for
We can loop through elements of array using for loop, as shown in the following example. Use "${array[@]}" so that each element remains a separate value, even if it contains spaces.
Example
fruits=("apple" "banana" "cherry")
for element in "${fruits[@]}";
do
echo $element
done
Output
apple
banana
cherry
If you need both the index and the value, loop through "${!array[@]}" and read the value with that index.
fruits=("apple" "banana" "cherry")
for index in "${!fruits[@]}"; do
printf 'Index %s: %s\n' "$index" "${fruits[$index]}"
done
Output
Index 0: apple
Index 1: banana
Index 2: cherry
Update, Append, and Remove Bash Array Elements
After an array is created, you can update one element by assigning to its index, append new elements with +=, and remove an element with unset.
fruits=("apple" "banana" "cherry")
fruits[1]="blueberry"
fruits+=("date" "elderberry")
unset 'fruits[0]'
printf '%s\n' "${fruits[@]}"
printf 'Indexes: %s\n' "${!fruits[@]}"
Output
blueberry
cherry
date
elderberry
Indexes: 1 2 3 4
Notice that removing index 0 does not automatically renumber the rest of the array. Bash arrays can be sparse, so the number of assigned elements and the highest index are not always the same thing.
Bash Associative Arrays with String Keys
Use an associative array when the value is easier to find by a name or key instead of a number. Associative arrays are declared with declare -A and require Bash 4 or newer.
declare -A fruit_colors
fruit_colors[apple]="red"
fruit_colors[banana]="yellow"
fruit_colors[cherry]="dark red"
echo "${fruit_colors[banana]}"
Output
yellow
For indexed arrays, use declare -a. For associative arrays, use declare -A. The uppercase A matters.
Multidimensional Bash Arrays Using Associative Keys
Bash does not provide native multidimensional arrays like some programming languages. A common workaround is to use an associative array with a combined key such as row,column.
declare -A table
table["0,0"]="Name"
table["0,1"]="Score"
table["1,0"]="Alice"
table["1,1"]="95"
echo "${table[1,0]}: ${table[1,1]}"
Output
Alice: 95
This approach is useful for small scripts, but for complex table-like data, a text format such as CSV, JSON, or a dedicated tool may be easier to maintain.
Common Bash Array Mistakes to Avoid
- Do not put spaces around
=while assigning an array. - Use
"${array[@]}"instead of unquoted${array[@]}when values may contain spaces. - Remember that Bash array indexing starts at
0. - Do not assume that
${#array[@]}is the same as the next available numeric index after usingunset. - Use
declare -Aonly for associative arrays, and check the Bash version when running scripts on older systems.
Bash Array Review Checklist for Scripts
- Array initialization uses parentheses and has no spaces around the assignment operator.
- Array expansions are quoted where filenames, paths, or values with spaces are possible.
- Loops use
"${array[@]}"for values or"${!array[@]}"for indexes, depending on what the script needs. - Code that removes elements handles sparse indexes correctly.
- Associative array examples use
declare -Aand are not mixed with indexed array syntax accidentally.
Bash Array FAQs
Are Bash arrays zero based?
Yes. Indexed Bash arrays start at index 0. For example, in fruits=("apple" "banana"), ${fruits[0]} is apple and ${fruits[1]} is banana.
How do I print all elements of a Bash array?
Use printf '%s\n' "${array[@]}" when you want each element on a separate line. Use echo "${array[@]}" only when a simple single-line display is enough.
What is the difference between “${array[@]}” and “${array[*]}”?
When quoted, "${array[@]}" expands each array element as a separate word, which is usually correct for loops. "${array[*]}" expands all elements as one joined string.
Can a Bash array contain both strings and numbers?
Yes. Bash treats array values as shell words or strings. A numeric-looking value can be stored beside text values, but arithmetic requires explicit arithmetic syntax such as $(( ... )).
Does Bash support multidimensional arrays?
Bash does not have native multidimensional indexed arrays. For small scripts, you can simulate them with associative array keys such as "row,column".
Summary of Bash Array Usage
In this Bash Tutorial, we learned how to declare, initialize, access, print, loop through, update, append, and remove Bash array elements. For most scripts, remember three habits: use zero-based indexes, quote array expansions, and use "${array[@]}" when looping through values.
TutorialKart.com