Bash For Loop Array: Iterate Through Array Values

Bash For Loop Array: Iterate Through Array Values

Body
How do I use bash for loop to iterate thought array values under UNIX / Linux operating systems? How can I loop through an array of strings in Bash? The Bash provides one-dimensional array variables. Any variable may be used as an array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based. This page explains how to declare a bash array and then use Bash for Loop to iterate through array values.
To declare an array in bash
Let us declare an array called array and assign three values when using bash:
array=( one two three )
Of course you can use the declare command as follows too:
# Indexed arrays (numbered list starting from zero )#
declare -a ARRAY_NAME_HERE=(value1 value2 .... valueN)
declare -a nameservers=(google cloudflare isp1)
nameservers[3]=isp2
Here are some more examples:
files=( "/etc/passwd" "/etc/group" "/etc/hosts" )
limits=( 10 20 26 39 48)

A note about bash associative array (key/value pair)

The syntax is as follows:
# associative arrays #
declare -A ARRAY_NAME_HERE
declare -A fruits
fruits[south]="Banana"
fruits[north]="Orange"
fruits[west]="Passion Fruit"
fruits[east]="Pineapple"

Printing an array under bash

To print an array use the printf command or echo command:
printf "%s\n" "${array[@]}"
printf "%s\n" "${files[@]}"
printf "%s\n" "${limits[@]}"
printf "%s\n" "${fruits[@]}"
#
# You can access elements using index or key values too under bash
#
printf "Group file in Linux or Unix: %s\n" "${files[1]}"
printf "Popular fruit in South India: %s\n" "${fruits[south]}"
Linux Loop through an array of strings in Bash

Click to enlarge

Bash for loop array example to iterate through array values

Use bash for loop syntax as follows:
for i in "${arrayName[@]}"
do
   : 
   # do whatever on "$i" here
done
The $i variable will hold each item in an array. Do not skip double quotes around the ${arrayName[@]}.

Loop through an array of strings in Bash

Here is a sample working script:
#!/bin/bash
# declare an array called array and define 3 vales
array=( one two three )
for i in "${array[@]}"
do
	echo "$i"
done
Run it as follows: chmod +x array_demo.sh ./array_demo.sh
Linux Bash For Loop Array: Iterate Through Array Values

Array Loops in Bash (click to enlarge)

Setting up a counter and iterate through bash array values

The following is another syntax or method one can use. So, let us declare an array called mahabharata as follows:
declare -a mahabharata
  Then add values or characters from the epic poem:
mahabharata[0]="Krishna"
mahabharata[1]="Yudhishthira"
mahabharata[2]="Bheema"
mahabharata[3]="Arjuna"
mahabharata[4]="Draupadi"
mahabharata[5]="Duryodhana"
Next get or find bash shell array length ( number of elements ) using the following syntax:
length=${#mahabharata[@]}
echo "Bash array '\${mahabharata}' has total ${length} element(s) (length)"
Finally use the three-expression (C style) bash for loops syntax:
for (( j=0; j<${length}; j++ ));
do
  printf "Current index %d with value %s\n" $j "${mahabharata[$j]}"
done
Here is how it looks:
#!/usr/bin/env bash
# declare an array variable
declare -a mahabharata=("Krishna" "Yudhishthira" "Bheema" "Arjuna" "Draupadi" "Duryodhana")
 
# get length of an array
length=${#mahabharata[@]}
 
# use C style for loop syntax to read all values and indexes
for (( j=0; j<length; j++ ));
do
  printf "Current index %d with value %s\n" $j "${mahabharata[$j]}"
done