Vote Up Down
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.
Of course you can use the declare command as follows too:
Here are some more examples:
The $i variable will hold each item in an array. Do not skip double quotes around the ${arrayName[@]}.
Run it as follows:
Join Patreon ➔
Then add values or characters from the epic poem:
Next get or find bash shell array length ( number of elements ) using the following syntax:
Finally use the three-expression (C style) bash for loops syntax:
Here is how it looks:
To declare an array in bash
Let us declare an array called array and assign three values when using bash:
array=( one two three )
# Indexed arrays (numbered list starting from zero )# declare -a ARRAY_NAME_HERE=(value1 value2 .... valueN) declare -a nameservers=(google cloudflare isp1) nameservers[3]=isp2
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]}"
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
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
chmod +x array_demo.sh
./array_demo.sh
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
Patreon supporters only guides ?
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
- If your domain is not sending email, set these DNS settings to avoid spoofing and phishing
mahabharata[0]="Krishna" mahabharata[1]="Yudhishthira" mahabharata[2]="Bheema" mahabharata[3]="Arjuna" mahabharata[4]="Draupadi" mahabharata[5]="Duryodhana"
length=${#mahabharata[@]} echo "Bash array '\${mahabharata}' has total ${length} element(s) (length)"
for (( j=0; j<${length}; j++ )); do printf "Current index %d with value %s\n" $j "${mahabharata[$j]}" done
#!/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