Looping Arrays
In this page:
Looping Arrays
for item in "${array[@]}" iterates directly over an array's values, which is the most idiomatic way to process every element. Looping by index instead, with for i in "${!array[@]}", is useful when you need the position as well as the value. Quoting "${array[@]}" prevents elements containing spaces from being split apart.
Note: Prefer for item in "${array[@]}" for simple value iteration; use the index form only when you need the index too.
Example: Looping Arrays
#!/bin/bash
tasks=("Write code" "Run tests" "Deploy")
for task in "${tasks[@]}"; do
echo "- $task"
done
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: