Problem Statement
What does a while loop do in bash?
Explanation
While loop executes commands repeatedly as long as condition is true (returns 0). Syntax: while [ condition ]; do commands; done. Example:
```bash
COUNT=0
while [ $COUNT -lt 5 ]; do
echo $COUNT
((COUNT++))
done
```
Tests condition before each iteration, may never execute if initially false.
Read file line by line (common pattern): while IFS= read -r line; do echo "$line"; done < file.txt. IFS= preserves leading/trailing whitespace, -r prevents backslash interpretation. Read from command: command | while read var; do echo "$var"; done. Note: variables modified in pipeline while loop don't affect parent shell (runs in subshell).
Infinite loop: while true; do commands; done or while :; do commands; done. Exit with break or when condition becomes false. Until loop is opposite: until [ condition ]; do commands; done executes while condition is false, stops when true.
Use while for condition-based iteration (unknown number of iterations), counters with complex logic, reading files line-by-line, or waiting for conditions. Understanding while loops enables flexible iteration based on dynamic conditions.
