## declare an array variable
declare -a arr=("element 1" "element 2" "element 3")

## loop through above array (quotes are important if your elements may contain spaces)
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

Also works for multi-line array declaration

declare -a arr=("element1" 
                "element2" "element3"
                "element4"
                )
Answer from anubhava on Stack Overflow
🌐
nixCraft
cyberciti.biz › nixcraft › howto › bash shell › bash for loop array: iterate through array values
Bash For Loop Array: Iterate Through Array Values - nixCraft
October 30, 2024 - Bash for loop array examples and syntax usage. Learn how to access each array item using a for loop to iterate through array values on Linux
Discussions

bash - How do I select an array to loop through from an array of arrays? - Unix & Linux Stack Exchange
I want to select with select an array to loop through from an array of arrays (aoarrs). The reason why I want to use select is because in the real world my array of arrays may have many more than just two arrays in it. How might I accomplish that? ... You really don't want to use CAPS for shell ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
bash - In a loop over an array, add an element to the array - Unix & Linux Stack Exchange
I have a problem with for loop in bash. For example: I have an array ("etc" "bin" "var"). And I iterate on this array. But in the loop I would like append some value to the array. E.g. array=("etc... More on unix.stackexchange.com
🌐 unix.stackexchange.com
shell script - Using a for loop to loop over multiple arrays in bash - Unix & Linux Stack Exchange
Bash doesn't natively support two-dimensional arrays, but I would like to simulate one. As a minimal working example, suppose that I have two arrays, a0 and a1: ... I want to write a for loop that will print the third element of a0 and a1. More on unix.stackexchange.com
🌐 unix.stackexchange.com
November 5, 2014
shell script - Bash - Looping through nested for loop using arrays - Unix & Linux Stack Exchange
Stack Exchange network consists ... for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I have 1 array and 2 associative array. I want to loop through both ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
People also ask

How do I loop over command-line arguments?
Use `for arg in "$@"; do` to iterate over all positional parameters passed to the script.
🌐
linuxize.com
linuxize.com › home › bash › bash for loop: syntax and examples
Bash For Loop: Syntax and Examples | Linuxize
What is the difference between the standard `for` loop and the C-style `for` loop?
The standard `for` loop iterates over a list of items (strings, array elements, a range). The C-style `for` loop uses arithmetic initialization, condition, and step expressions — it is better suited for counter-based iteration where you control the start, end, and step explicitly.
🌐
linuxize.com
linuxize.com › home › bash › bash for loop: syntax and examples
Bash For Loop: Syntax and Examples | Linuxize
How do I loop over lines in a file?
Use a `while` loop with `read` instead: `while IFS= read -r line; do echo "$line"; done < file.txt`. A `for` loop is not suited for line-by-line file reading because it splits on whitespace, not newlines.
🌐
linuxize.com
linuxize.com › home › bash › bash for loop: syntax and examples
Bash For Loop: Syntax and Examples | Linuxize
🌐
KodeKloud
kodekloud.com › blog › bash-scripts-loop-through-array-values
How to Write Bash Scripts to Loop Through Array Values
January 21, 2026 - It loops through a list of items and performs a set of commands for each item. The list of items can be a series of strings, numbers, or elements of an array. Here's what the Bash syntax looks like: ... In this syntax, item is a variable that represents the current item in the item_list for each iteration of the loop.
🌐
Linux Handbook
linuxhandbook.com › bash-for-loop-array
Use For Loop With Array in Bash
June 22, 2023 - This method uses a typical for loop where you declare the initial value, a condition, and if the condition is true then an action (increment or decrement). ... In the above syntax, I used i as a variable and will print each element till the ...
🌐
Linuxize
linuxize.com › home › bash › bash for loop: syntax and examples
Bash For Loop: Syntax and Examples | Linuxize
March 2, 2026 - In the example below, we define an array named BOOKS and iterate over each element of the array. ... BOOKS=('In Search of Lost Time' 'Don Quixote' 'Ulysses' 'The Great Gatsby') for book in "${BOOKS[@]}"; do echo "Book: $book" done ... The INITIALIZATION part is executed only once when the loop starts. Then, the TEST part is evaluated using comparison operators . If it is false, the loop is terminated.
Find elsewhere
🌐
iO Flood
ioflood.com › blog › bash-loop-through-array
Bash Script Array Looping | How to Iterate Through Values
December 4, 2023 - This is a fundamental technique in Bash scripting that allows you to process each element in an array individually. ... array=('item1' 'item2' 'item3') for i in "${array[@]}" do echo "$i" done # Output: # item1 # item2 # item3 · In this example, ...
🌐
nixCraft
cyberciti.biz › nixcraft › howto › bash shell › bash iterate array examples
Bash Iterate Array Examples - nixCraft
November 27, 2023 - Bash iterate array examples under Linux / UNIX / *BSD / Mac OS (OS X) using bash for loop syntax for an indexed/associative array.
🌐
Medium
aoyilmaz.medium.com › bash-scripting-for-beginners-part-2-arrays-and-loops-680b302d9643
Bash Scripting for Beginners (Part 2 — Arrays and Loops) | by Ahmet Okan YILMAZ | Medium
April 11, 2022 - #!/bin/usr/env bash for n in {1..10..2}; do echo $n done ... If you don’t specify increment option, the loop increments by 1. ... You can also use the for loop to iterate over an array of elements.
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › array-basics-shell-scripting-set-2-using-loops
Array Basics Shell Scripting | Set 2 (Using Loops) - GeeksforGeeks
November 18, 2025 - The for...in loop iterates over each value in your array. ... #!/bin/bash # 1. DEFINE the array servers=("web-01" "db-01" "app-01") # 2.
Top answer
1 of 4
6

You'll store the array names in aoarrs, and inside the select body declare a nameref to the chosen name:

ARGENT=("Nous devons économiser de l'argent."
"Je dois économiser de l'argent.")
BIENETRE=("Comment vas-tu?" "Tout va bien ?")
aoarrs=(ARGENT BIENETRE)

PS3='Which array? '
select arr in "${aoarrs[@]}"; do 
    [[ $arr ]] || continue
    declare -n ref=$arr
    for i in "${!ref[@]}"; do 
        printf '%d\t%s\n' $i "${ref[i]}"
    done 
    break
done

Running might look like

1) ARGENT
2) BIENETRE
Which array? 3
Which array? 4
Which array? 5
Which array? 2
0   Comment vas-tu?
1   Tout va bien ?
2 of 4
2

You'd want a mapping of "keys" to "values", where "values" are the lists of strings, and the "keys" are ARGENT, BIENETRE

You're on the right path with aoarrs, because you could use that array as associative array:

declare -A aoarrs
aoarrs[ARGENT]=$ARGENT
aoarrs[BIENETRE]=$BIENETRE

and then just iterate over all keys in that array using something like for key in ${!aoarrs[@]}….

Sadly, bash doesn't, for whatever reason, allow lists to be elements of these associative arrays.

So, things suck. You can for example join the elements of your lists with a reserved character to split them later on (that's stupid because it means you can't have all characters in your string, or need to start escaping them), or you build your own functions that take lists of strings, append them to a large array and then you implement your own associative lookup function on that container (that would be stupid; not only would it be slow, it would also require you to write relatively much code in a relatively ill-suited language). It would look terrible. Here's an example which I write down without testing it, because it's ugly enough that I need to get it out of my head, but don't want to deal with it any further:

#!/bin/bash
###############################
#        UNTESTED STUFF       #
# If you think this code is   #
# acceptable, consider doing  #
# something good for yourself #
# once in a while             #
###############################
declare -A index
declare -A lengths
declare -a storage

# Adds an entry to our our custom container
# 
# Usage:
# add_key_values KEY "list element 1" "list element 2" …
function add_key_values() {
  local key="$1"
  shift
  local -n valuelist=$@

  # get the length of the passed list, to save it
  local lengthlist=${#valuelist[@]}


  # get the end of the current storage, that's where we start adding
  # our list
  local start_index=${#storage[@]}

  # finally, actually store the list items in the storage
  for item in "${valuelist[@]}"; do
    storage+=("${item}")
  done
  lengths["${key}"]=$lengthlist
  index["${key}"]=$start_index
}

# Retrieve a list from the storage
# Sadly, bash is not a proper programming language, because what it calls
# "functions" don't do the one thing that a function needs to do:
# return a value for an argument. There's just no "return" mechanism in bash.
# 
# Returns an empty list if the key wasn't found.
#
# So, after sobbing a bit, we just say
# Usage:
# get_values $key
# Overwrites the `ret_val` variable with the list that was saved earlier
function get_values() {
  # prepare ret_val
  declare -g -a ret_val=()

  local key=$1

  # We return (with ret_val empty) if key isn't present
  # frigging bash doesn't have a "is key present in associative array" function…
  # so this is the workaround to check whether there's $key in $index.
  # seriously.
  local teststring
  teststring="$(printf 'index[%s]' "${key}")"
  # you need quite modern bash to even get the "is defined" -v test
  [[ -v "${teststring}" ]] || return

  # let's get all the elements from storage and append them to ret_val
  local start=${index[$key]}
  local length=${lengths[$key]}
  for idx in $(seq $(( start - 1 )) $((start - 1 + length)) ); do 
    ret_val+=("${storage[idx]}")
  done
}

####################
# EXAMPLE USAGE
####################
add_key_values "ARGENT" "Nous devons économiser de l'argent." "Je dois économiser de l'argent."
add_key_values "BIENETRE" ("Comment vas-tu?" "Tout va bien ?")

for key in ${!index[@]}; do
  echo "the list for value $key contains"
  get_values "${key}"
  for element in ${ret_val[@]}; do
    echo "element: ${element}"
  done
done

The next option is magic that involves "indirect addressing" of variables by name using eval. That's kind of evil, and stupid, and there's very many posts on here that hint at "if you're at that point, then maybe use a proper programming language instead of bash".

I'd concur with that: This whole problem would literally be done in four lines of python, with the first two lines would be storing "ARGENT" and "BIENETRE" and their lists into a dict. Or really, in any other common language that's not bash (or C, for that matter), associative arrays are less bad.

🌐
Stack Abuse
stackabuse.com › array-loops-in-bash
Array Loops in Bash
July 12, 2023 - Now that we have seen and understand the basic commands of the Bash shell as well as the basic concepts of loops and arrays in Bash, let's go ahead and see a useful script using the loops and arrays together. In our simple example below we'll assume that you want to display a list of your website's registered users to the screen. The list of users is stored in the variable users, which you need to loop over to display them. #!/bin/bash users=(John Harry Jake Scott Philis) for u in "${users[@]}" do echo "$u is a registered user" done
🌐
LabEx
labex.io › questions › how-to-loop-through-an-array-in-bash-388816
How to Loop Through an Array in Bash | LabEx
September 19, 2024 - Here's a Mermaid diagram that ... in Bash: graph TD A[Array] --> B(for item in "${array[@]}") A[Array] --> C(for i in "${!array[@]}") A[Array] --> D(while [ $i -lt ${#array[@]} ]) B --> E[Print item] C --> F[Print array[i]] D --> G[Print array[i]] D --> H[i=i+1]...
🌐
Linux.org
linux.org › home › forums › linux.org news, tutorials and articles › linux original content › linux articles › shell / command line
Bash 08 – Arrays and For Loops | Linux.org
June 25, 2022 - Let’s look at the structure of a For Loop. ... for element in list/array; do command_1 command_n done Let’s look at a small list first. The list will comprise four genres: sci-fi, fantasy, comedy and anime. With each genre, we will create a folder in a folder named ‘Movies’ that is in the Home folder under ‘Videos’. The script would look like: GNU nano 6.2 forloop.sh ... #!/bin/bash mkdir ~/Videos/Movies for folder in sci-fi fantasy comedy anime; do mkdir ~/Videos/Movies/$folder done cd ~/Videos/Movies ls Now, let’s look at this in depth.
🌐
LinuxSimply
linuxsimply.com › home › bash scripting tutorial › a complete guide to bash array › elements of bash array › 3 ways to iterate an array in bash
3 Ways to Iterate an Array in Bash
April 17, 2024 - After declaring the array users, the loop for item in ${users[@]} iterates the array to access each element individually within the item variable and prints them on the screen with the assistance of echo $item.
🌐
Shell Tips!
shell-tips.com › home › bash › a complete guide on how to use bash arrays
A Complete Guide on How To Use Bash Arrays
September 26, 2020 - The indexed arrays are sometimes called lists and the associative arrays are sometimes called dictionaries or hash tables. The support for Bash Arrays simplifies heavily how you can write your shell scripts to support more complex logic or to safely preserve field separation. This guide covers the standard bash array operations and how to declare (set), append, iterate over (loop...