Loop Through Array in Bash
Fumbani Banda
Oct 29, 2021

This tutorial loops through an array in bash and displays all values.
Loop Through an Array in Bash
We create an index array with the -a
option, which is named cities_array
. We loop through ${cities_array[@]}
, which contains all the values of the array, and we display each value with the printf
command.
declare -a cities_array=("New York" "London" "Moscow")
for city in "${cities_array[@]}"
do
printf "$city\n"
done
Output:
New York
London
Moscow
Author: Fumbani Banda
Related Article - Bash Array
- Return an Array in Bash
- Bash Associative Array
- Pass an Array to a Function in Bash
- Print Array Elements in Bash
- Append New Data to an Array Without Specifying the Index in Bash
- Multi-Dimensional Array in Bash
Related Article - Bash Loop
- Break Out of a Loop in Bash
- Create an Infinite Loop in Bash
- Implement the for Loop in Bash
- For Loop in Bash
- Create One Line Loop in Bash
- Loop Over Files in Directory in Bash