[Bash] Loop over array elements (foreach)

Created on November 28, 2026

You can use a for loop in Bash to iterate over each array element, as you would with a foreach loop. Each element of the array arr is assigned to the variable i in turn. Since arr contains three elements, the loop runs three times.

#! /bin/bash

arr=(a b c)
for i in "${arr[@]}"
do
    echo $i
done

The script produces the following output.

a
b
c
Bash Shell command