Bash Arrays

Arrays are one of the most commonly used and fundamental data structures. You can think of an array as a variable that stores multiple values, allowing you to represent and manipulate a group of related data as a single entity.
Bash supports two types of arrays: numerically indexed arrays referenced by integers, and associative arrays referenced by strings (keys). Unlike most programming languages, Bash array elements do not have to be the same data type — an array can contain both strings and numbers. Bash does not support multidimensional arrays.
Numerically indexed arrays can be accessed from the end using negative indices. The index -1 references the last element. Indices do not have to be contiguous.
Creating Arrays
Numerically Indexed Arrays
Bash variables are untyped, so any variable can be used as an indexed array without declaring it. To explicitly declare one, use the declare builtin:
declare -a array_nameYou can assign elements individually by index:
array_name[0]=value_1
array_name[1]=value_2
array_name[2]=value_3Or initialize the whole array at once using parentheses, with elements separated by spaces:
array_name=( element_1 element_2 element_3 )When using this form, indexing starts at zero.
Associative Arrays
Associative arrays use strings as keys instead of integers. Unlike indexed arrays, they must be declared before use:
declare -A array_nameAssign elements individually:
declare -A array_name
array_name[key_foo]=value_foo
array_name[key_bar]=value_bar
array_name[key_xyz]=value_xyzOr use the inline form:
declare -A array_name=(
[key_foo]=value_foo
[key_bar]=value_bar
[key_xyz]=value_xyz
)Access Elements
To reference a single element, use the following syntax:
${array_name[index]}${} delimit the variable name and index expression so Bash parses it correctly.For example, to print the element at index 1:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
echo ${my_array[1]}HeliumTo access all elements, use @ or * as the index:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
echo "${my_array[@]}"Hydrogen Helium Lithium BerylliumThe difference between @ and * matters when the expression is inside double quotes. "${my_array[*]}" expands to a single word with elements separated by spaces. "${my_array[@]}" expands each element as a separate word. Use @ when iterating over array elements to handle values that contain spaces correctly.
To print all keys (indices), add ! before the array name:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
echo "${!my_array[@]}"0 1 2 3Array Length
To get the number of elements in an array, prefix the array name with #:
${#array_name[@]}Here is an example:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
echo ${#my_array[@]}4Loop Through an Array
The for loop
is the most common way to iterate over array elements:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
for i in "${my_array[@]}"
do
echo "$i"
doneHydrogen
Helium
Lithium
BerylliumTo print both keys and values:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
for i in "${!my_array[@]}"
do
echo "$i" "${my_array[$i]}"
done0 Hydrogen
1 Helium
2 Lithium
3 BerylliumYou can also use a C-style loop with the array length:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
length=${#my_array[@]}
for (( i=0; i < ${length}; i++ ))
do
echo $i ${my_array[$i]}
done0 Hydrogen
1 Helium
2 Lithium
3 BerylliumAdd Elements
To add an element at a specific index:
my_array[index]="New Element"To append one or more elements without specifying an index, use the += operator:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
my_array+=( Cobalt Nickel )
echo "${my_array[@]}"Hydrogen Helium Lithium Beryllium Cobalt NickelSlice an Array
To extract a portion of an array, use the following syntax:
${array_name[@]:offset:length}offset— the starting indexlength— the number of elements to return
For example, to get two elements starting at index 1:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
echo "${my_array[@]:1:2}"Helium LithiumDelete Elements
To remove a single element by index, use unset:
declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" )
unset my_array[2]
echo "${my_array[@]}"Hydrogen Helium BerylliumNote that unset removes the element but does not re-index the array. The index gap remains.
To delete the entire array:
unset my_arrayQuick Reference
For a printable quick reference, see the bash cheatsheet .
| Task | Syntax |
|---|---|
| Declare indexed array | declare -a arr |
| Declare associative array | declare -A arr |
| Initialize array | arr=( a b c ) |
| Access element | ${arr[index]} |
| Access all elements | "${arr[@]}" |
| Access all keys | "${!arr[@]}" |
| Array length | ${#arr[@]} |
| Append elements | arr+=( x y ) |
| Slice array | ${arr[@]:offset:length} |
| Delete element | unset arr[index] |
| Delete entire array | unset arr |
| Last element | ${arr[-1]} |
FAQ
What is the difference between @ and * when accessing all elements?
Both expand to all array elements, but the behavior differs inside double quotes. "${arr[@]}" expands each element as a separate word, which correctly handles elements containing spaces. "${arr[*]}" expands to a single string with elements joined by the first character of IFS (usually a space). Always use @ when iterating.
How do I check if a Bash array is empty?
Check the length: [[ ${#my_array[@]} -eq 0 ]]. In a script:
if [[ ${#my_array[@]} -eq 0 ]]; then
echo "Array is empty"
fiHow do I check if a value exists in an array?
Loop through the array and compare each element
:
for i in "${my_array[@]}"; do
[[ "$i" == "Helium" ]] && echo "Found" && break
doneWhy does unset leave a gap in the array index?unset arr[2] removes the element at index 2 but does not shift the remaining elements. The index 2 simply becomes unset. To re-index, reassign: arr=( "${arr[@]}" ).
Can Bash arrays hold arrays as elements?
No. Bash does not support nested or multidimensional arrays. To simulate a 2D structure, use naming conventions such as arr_0_0, arr_0_1, or use associative arrays with compound keys like arr[0,1].
Conclusion
Bash arrays store multiple values in a single variable. Use indexed arrays for ordered lists and associative arrays for key-value pairs. The [@] expansion, ${#arr[@]} for length, += for appending, and unset for deletion are the core operations you will use most often.
For more on Bash scripting, see the guides on for loops and if/else statements .
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page