Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Created June 12, 2024 08:49
Show Gist options
  • Save nilforooshan/3710898a890ae6de3342675e08f1aac2 to your computer and use it in GitHub Desktop.
Save nilforooshan/3710898a890ae6de3342675e08f1aac2 to your computer and use it in GitHub Desktop.
sh: Loop Through Array Values

Loop Through Array Values

countries=("India" "France" "United Kingdom")

# Loop through the array
for country in "${countries[@]}"
do
   echo "$country"
done
India
France
United Kingdom
for index in "${!countries[@]}"
do
   echo "Index: $index, Value: ${countries[$index]}"
done
for index in "${!countries[@]}"
do
   echo "Index: $index, Value: ${countries[$index]}"
done

Source: https://kodekloud.com/blog/bash-scripts-loop-through-array-values/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment