Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A while loop in Bash is a control flow statement that repeatedly executes a block of commands as long as a specified test command (or list of commands) evaluates to true (returns an exit status of 0). The loop terminates immediately when the evaluated command returns a non-zero exit status.

Syntax

The standard multi-line syntax requires the while, do, and done keywords. Placeholders are denoted by angle brackets (< >):
while <test-commands>
do
    <commands>
done
For inline execution, statements must be separated by semicolons:
while <test-commands>; do <commands>; done

Condition Evaluation Mechanics

Unlike many high-level languages that evaluate boolean expressions, a Bash while loop evaluates the exit status of a command or a list of commands. Crucially, <test-commands> is not limited to a single command. It can be a pipeline or a list of multiple commands separated by ;, &&, or ||. When a list of commands is provided, the while loop evaluates the exit status of the last command executed in that list.
  • Exit Status 0: Interpreted as success (true). The loop executes the do block.
  • Exit Status >0: Interpreted as failure (false). The loop terminates, and execution continues after the done keyword.
The <test-commands> typically utilize one of the following evaluation constructs:
  1. Command Lists: Multiple commands where the final command dictates the loop continuation.
while echo "Checking..."; ping -c 1 10.0.0.1 > /dev/null; do
  1. POSIX Test Command ([ ] or test): Used for standard file, string, and integer comparisons.
while [ "$var" -lt 10 ]; do
  1. Bash Extended Test ([[ ]]): Provides advanced string manipulation, pattern matching, and logical operators without word splitting issues.
while [[ "$string" != *"pattern"* ]]; do
  1. Arithmetic Evaluation ((( ))): Used strictly for C-style integer arithmetic and mathematical comparisons.
while (( var < 10 )); do
  1. Standard Commands: Any executable command or pipeline. The loop continues as long as the command executes successfully.
while grep -q "active" status.log; do

Loop Control Statements

Bash provides two built-in commands to alter the standard execution flow within the do block:
  • break: Immediately terminates the loop entirely, bypassing the condition check, and transfers control to the command following done. It accepts an optional integer argument (e.g., break 2) to break out of nested loops.
  • continue: Halts the current iteration, skips any remaining commands in the do block, and returns control to the top of the loop to re-evaluate the while condition.

Infinite Loops

An infinite loop is created by providing a condition command that always returns an exit status of 0. This is typically achieved using the true command or the : (null utility) built-in, which does nothing and always succeeds.
while :
do
    <commands>
done

Input Redirection

The while loop can process data streams by redirecting Standard Input (stdin) directly into the loop structure. This is most commonly paired with the read built-in command, which returns 0 as long as it successfully reads a line.
while IFS= read -r line
do
    <commands>
done < "filename.txt"
In this construct, the redirection < "filename.txt" is applied to the entire while loop block, making the file’s contents available to the read command on each iteration until the End-of-File (EOF) is reached, at which point read returns a non-zero exit status and the loop terminates.
Master Bash with Deep Grasping Methodology!Learn More