|
|
|
The |
|
|
Basic while loop counting from 1 to 5: |
|
|
The condition is tested before each iteration. If false initially, the loop body never runs. |
|
|
Countdown example: |
|
|
While loops are great for reading files line by line: |
|
|
The |
|
|
Infinite loop with break: |
|
|
The |
|
|
Reading from a command’s output: |
|
|
Note: Variables modified inside a piped while loop won’t persist outside due to subshell behavior. |
|
|
Use process substitution or here-string to avoid subshells. In POSIX sh, redirect from a temp file or use a different approach. |
|
|
While loop with multiple conditions: |
|
|
The |
|
|
Menu-driven loop example: |
|
|
Processing arguments with while and shift: |
|
|
While loop for retry logic: |
|