> ## 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 For Loop

A `for` loop in Bash is a control flow construct used to iterate over a sequence of elements or to execute a block of code a specified number of times. Bash supports two distinct syntactic forms for the `for` loop: the standard POSIX list-iteration loop and the C-style arithmetic loop.

## Standard List-Iteration Loop

The list-iteration loop executes a block of commands for each member of a provided list.

```bash theme={"dark"}
for variable_name in word_list; do
    # commands to execute
done
```

**Mechanics:**

1. **Shell Expansions:** Before the loop initiates, the shell evaluates the `word_list` through a strict sequence of expansions to generate the final list of items:
   * **Initial Expansions:** The shell first performs brace expansion, followed by tilde expansion, parameter and variable expansion, command substitution, and arithmetic expansion.
   * **Word Splitting:** The shell scans the results of the expansions. It applies word splitting based on the Internal Field Separator (`$IFS`) *only* to the unquoted results of parameter expansion, command substitution, and arithmetic expansion. It does not split the results of brace expansion, tilde expansion, or literal strings.
   * **Pathname Expansion:** After word splitting, the shell performs pathname expansion (globbing) on the resulting words to resolve file patterns (e.g., `*`, `?`).
   * **Quote Removal:** As the final step, all unquoted backslashes, single quotes, and double quotes that did not result from the previous expansions are removed.
2. **Assignment and Execution:** For each resulting word from the final expanded list, the shell assigns the word's value to `variable_name` and executes the commands enclosed between the `do` and `done` keywords.
3. **Implicit Iteration:** If the `in word_list` clause is omitted entirely (`for variable_name; do`), the loop implicitly iterates over the positional parameters (`"$@"`), which represent the arguments passed to the script or function.

## C-Style Arithmetic Loop

The C-style loop utilizes Bash's arithmetic evaluation compound command `((...))` to control iteration through mathematical expressions.

```bash theme={"dark"}
for (( expr1; expr2; expr3 )); do
    # commands to execute
done
```

**Mechanics:**

1. **Initialization (`expr1`):** This arithmetic expression is evaluated exactly once before the loop begins. It is typically used to initialize a loop counter variable.
2. **Condition (`expr2`):** This expression is evaluated before each iteration. It acts as the loop's continuation condition. If the arithmetic evaluation results in a non-zero value (representing boolean true in C-style logic), the loop executes. If it evaluates to zero (boolean false), the loop terminates. If `expr2` is omitted, it evaluates to true.
3. **Step (`expr3`):** This expression is evaluated at the end of each iteration, immediately after the commands within the `do`...`done` block have executed. It is typically used to increment or decrement the loop counter.

## Loop Control Statements

The execution flow within both types of `for` loops can be modified using built-in control statements:

* **`break [n]`**: Immediately terminates the loop. If `n` is specified, it breaks out of `n` nested loops.
* **`continue [n]`**: Skips the remaining commands in the current iteration. In a list-iteration loop, it proceeds to the next word in the list. In a C-style loop, it proceeds directly to the evaluation of the step expression (`expr3`). If `n` is specified, it resumes iteration at the `n`th enclosing loop.

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