for Loop
In this page:
for Loop
A for loop in Bash iterates over a list of words, a range with {1..5}, or a C-style numeric range with ((i=0; i<n; i++)). The classic form, for item in list; do ... done, is the most common style. Bash's for is more flexible than the pure counting loop found in many other languages.
Note: for i in {1..10} is a concise way to iterate over a fixed numeric range without a C-style loop.
Example: for Loop
#!/bin/bash
for i in {1..5}; do
echo "Number: $i"
done
for ((i = 0; i < 3; i++)); do
echo "C-style: $i"
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: