while
Execute commands repeatedly while a condition is true
TLDR
Read stdin and perform an action on every line
Execute a command forever once every second
Execute a command until it fails
SYNOPSIS
while condition_commands;
do
loop_body_commands
done
PARAMETERS
condition_commands
One or more commands whose exit status determines whether the loop continues. If the last command in this sequence exits with status 0 (success), the loop body is executed. Otherwise, the loop terminates.
loop_body_commands
The block of commands that will be executed repeatedly as long as the condition_commands evaluate to true. These commands are executed sequentially.
DESCRIPTION
The while loop is a fundamental control flow construct in Linux shell scripting. It enables the repeated execution of a block of commands as long as a specified condition remains true. The condition is determined by the exit status of a command or a series of commands: an exit status of 0 (success) causes the loop to continue, while any non-zero exit status (failure) terminates the loop.
This construct is essential for automation tasks requiring iteration, such as processing lines from files, waiting for processes to complete, or continuous monitoring. The loop first evaluates the condition. If true, the commands within the loop body are executed. After execution, the condition is re-evaluated, and the process repeats until the condition becomes false.
CAVEATS
Infinite Loops: Care must be taken to ensure that the loop's condition eventually becomes false; otherwise, an infinite loop will occur, consuming system resources. Always include logic within the loop body or condition that can alter the condition's outcome.
Subshell Execution with Pipes: When a while loop is part of a pipeline (e.g., 'cat file | while read line; do ...; done'), the while loop often runs in a subshell. This means that any variables modified inside the loop will not be available or retain their changes outside the pipeline. To avoid this, consider process substitution (e.g., 'while read line; do ...; done < file') or redirecting input directly.
COMMON USE CASES
Processing Files Line-by-Line: A common pattern is 'while IFS= read -r line; do ...; done < file.txt' for safe and reliable line processing.
Waiting for Events: Loops can be used to poll for a condition to become true, e.g., 'while ! pgrep my_process; do sleep 5; done'.
Reading User Input: Continuously prompt for input until valid data is provided.
LOOP CONTROL
Two commands can alter loop execution:
break: Immediately terminates the innermost while loop and execution continues with the command following the done keyword.
continue: Skips the rest of the current iteration of the innermost while loop and proceeds to the next iteration by re-evaluating the condition.
HISTORY
The while loop is a fundamental control flow construct that originated with the Bourne Shell (sh) in Unix. It has been a core part of shell scripting since its inception and is available in virtually all modern Unix-like shells, including Bash, Zsh, Ksh, and Dash. Its design leverages the Unix philosophy of using command exit statuses for conditional logic, making it a robust and versatile tool for iterative tasks.