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

A Bash pipeline is an inter-process communication (IPC) mechanism that chains multiple commands together by connecting the standard output (`stdout`) of one process directly to the standard input (`stdin`) of the next process using anonymous pipes.

## Syntax

```bash theme={"dark"}

# Standard pipeline (stdout to stdin)
command1 | command2 | command3


# Standard error and standard output pipeline (stdout + stderr to stdin)
command1 |& command2
```

## Technical Mechanics

**File Descriptor Mapping**

* The standard pipe operator (`|`) connects File Descriptor 1 (FD 1, `stdout`) of the preceding command to File Descriptor 0 (FD 0, `stdin`) of the succeeding command.
* The `|&` operator is a Bash shorthand introduced in version 4.0. It connects both FD 1 (`stdout`) and FD 2 (`stderr`) of the preceding command to FD 0 (`stdin`) of the succeeding command. It is functionally equivalent to `command1 2>&1 | command2`.

**Execution Environment (Subshells)**
By default, Bash executes every command within a pipeline in its own separate subshell environment. Consequently, variable assignments, state changes, or directory changes (`cd`) made by any command within the pipeline will not persist in the parent shell once the pipeline terminates.

*Note: In Bash 4.2+, enabling `shopt -s lastpipe` allows the final command of a pipeline to execute in the current shell environment, provided job control is disabled.*

**Concurrency and I/O Blocking**
Commands in a pipeline are invoked concurrently, not sequentially. The operating system kernel allocates a bounded memory buffer for each anonymous pipe.

* The writing process will block (suspend execution) if the pipe buffer is full.
* The reading process will block if the pipe buffer is empty.
* The pipeline terminates when all constituent processes have terminated.

**Exit Status**
By default, the exit status (`$?`) of an entire pipeline is strictly the exit status of the *last* (rightmost) command in the chain. The exit statuses of preceding commands are discarded.

To alter this behavior, Bash provides the `pipefail` option:

```bash theme={"dark"}
set -o pipefail
```

When `pipefail` is enabled, the pipeline's exit status becomes the exit status of the rightmost command to exit with a non-zero status. If all commands in the pipeline exit successfully (status `0`), the pipeline returns `0`.

**Array of Exit Codes**
Bash retains the individual exit statuses of all commands executed in the most recent pipeline within the internal array variable `PIPESTATUS`.

```bash theme={"dark"}
command1 | command2 | command3
echo "${PIPESTATUS[@]}" # Outputs an array of three exit codes, e.g., "0 1 0"
```

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