How to iterate through an array?

QuestionsQuestions4 SkillsProShell ArraysOct, 25 2025
0178

To iterate through an array in Bash, you can use a for loop. Here's a simple example:

#!/bin/bash

# Initialize an array
FRUITS=("apple" "banana" "cherry")

# Iterate through the array
for fruit in "${FRUITS[@]}"; do
    echo "$fruit"
done

Explanation:

  • FRUITS=("apple" "banana" "cherry") initializes an array with three fruit names.
  • The for loop iterates over each element in the array using "${FRUITS[@]}", which expands to all elements.
  • echo "$fruit" prints each fruit to the console.

Feel free to ask if you need more details or examples!

0 Comments

no data
Be the first to share your comment!