Bash For Loop Array: Iterate Through Array Values

CategoriesSoftware Development

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)

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 fruits
fruits[west]="Passion Fruit"

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 whatever on "$i" here

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:

# declare an array called array and define 3 vales
array=( one two three )
for i in "${array[@]}"
	echo "$i"

Run it as follows:
chmod +x

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:


Next get or find bash shell array length ( number of elements ) using the following syntax:

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++ ));
  printf "Current index %d with value %s\n" $j "${mahabharata[$j]}"

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
# use C style for loop syntax to read all values and indexes
for (( j=0; j<length; j++ ));
  printf "Current index %d with value %s\n" $j "${mahabharata[$j]}"

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.