Iterating through List

2 minute read

Iterating through lists is a fundamental skill in Bash scripting, enabling you to process and manipulate data efficiently. In this tutorial, we’ll explore various methods to iterate through lists, providing practical examples and addressing common questions to help you navigate and master list processing in Bash.

Basic Iteration with For Loop


# Basic iteration with for loop

fruits=("Apple" "Orange" "Banana" "Grapes")

for fruit in "${fruits[@]}"; do
  echo "Fruit: $fruit"

This example illustrates a basic for loop to iterate through each element in the list (fruits) and print its value.

Iterating with For Loop and Indices


# Iterating with for loop and indices

colors=("Red" "Green" "Blue")

for index in "${!colors[@]}"; do
  echo "Color at index $index: ${colors[$index]}"

Here, the loop iterates through indices of the list (colors) using "${!colors[@]}" and prints the corresponding values.

Iterating with While Loop


# Iterating with while loop

numbers=(1 2 3 4 5)


while [ $counter -lt ${#numbers[@]} ]; do
  echo "Number: ${numbers[$counter]}"

This example demonstrates iterating through the list (numbers) using a while loop and a counter variable.

Advanced Iteration with Command Substitution


# Advanced iteration with command substitution


for file in $files_list; do
  echo "File: $file"

In this example, the list of files in the current directory is obtained through command substitution ($(ls)) and iterated using a for loop.


  1. How can I iterate through a list and perform a specific action for each element?
    Use a for loop, specifying the list and the action to be performed inside the loop.

  2. Is it possible to iterate through a list of numbers and perform arithmetic operations?
    Yes, you can iterate through a list of numbers using a loop and perform various arithmetic operations within the loop.

  3. What is the purpose of "${!list[@]}" in a for loop?
    The "${!list[@]}" expands to the indices of the array, allowing you to iterate through the indices of the list.

  4. Can I use a while loop to iterate through a list in Bash?
    Yes, a while loop can be used to iterate through a list by employing a counter variable.

  5. How do I iterate through the output of a command in Bash?
    Use command substitution ($(command)) to capture the output of a command and iterate through it using a for loop.

  6. What is the significance of ((counter++)) in a while loop for list iteration?
    The ((counter++)) is used to increment the counter variable within the while loop, ensuring the loop progresses through the list.

  7. Can I iterate through a list and perform conditional actions based on the elements?
    Yes, within a loop, you can use conditional statements to perform specific actions based on the values of the list elements.

  8. Is there a limit to the size of the list that can be iterated in Bash?
    The size of the list in Bash is not strictly limited, but practical considerations like available memory can impact the performance of large lists.

  9. What happens if the list is empty when iterating through it?
    If the list is empty, a for or while loop will not execute, and no iterations will occur.

  10. How can I iterate through a list of strings with spaces in Bash?
    Ensure that each element in the list is properly quoted, and the loop will handle strings with spaces correctly. For example, use "${list[@]}" to preserve spaces in string elements.