Definition |
Executes a set of commands for each item in a list or range. |
Executes a set of commands as long as a condition is true. |
Iterations |
Runs for a known number of times or predefined list. |
Runs for an unknown number of times, depends on the condition. |
Control |
Iterates through values provided (e.g., numbers, filenames). |
Continuously checks a condition before every iteration. |
Termination |
Stops after completing all values in the list. |
Stops when the condition becomes false. |
Best Use Case |
Useful for looping through ranges, arrays, or command outputs. |
Useful for reading input, waiting for events, or monitoring. |
Syntax |
for var in list; do commands; done |
while [ condition ]; do commands; done |
Example |
for num in 1 2 3 4 5
do
echo "Number: $num"
done
|
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count+1))
done
|
Output (Example) |
Number: 1 … Number: 5 |
Count: 1 … Count: 5 |