> ## 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.

# Bash While Loop

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 (`< >`):

```bash theme={"dark"}
while <test-commands>
do
    <commands>
done
```

For inline execution, statements must be separated by semicolons:

```bash theme={"dark"}
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.

```bash theme={"dark"}
while echo "Checking..."; ping -c 1 10.0.0.1 > /dev/null; do
```

2. **POSIX Test Command (`[ ]` or `test`):** Used for standard file, string, and integer comparisons.

```bash theme={"dark"}
while [ "$var" -lt 10 ]; do
```

3. **Bash Extended Test (`[[ ]]`):** Provides advanced string manipulation, pattern matching, and logical operators without word splitting issues.

```bash theme={"dark"}
while [[ "$string" != *"pattern"* ]]; do
```

4. **Arithmetic Evaluation (`(( ))`):** Used strictly for C-style integer arithmetic and mathematical comparisons.

```bash theme={"dark"}
while (( var < 10 )); do
```

5. **Standard Commands:** Any executable command or pipeline. The loop continues as long as the command executes successfully.

```bash theme={"dark"}
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.

```bash theme={"dark"}
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.

```bash theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
