> ## 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 Bitwise OR

The `|` (pipe) operator is an inter-process communication (IPC) mechanism that connects the standard output (`stdout`, file descriptor 1) of a preceding command directly to the standard input (`stdin`, file descriptor 0) of a subsequent command. It facilitates the unidirectional flow of byte streams between processes via kernel memory buffers, eliminating the need for intermediate temporary files.

## Syntax

```bash theme={"dark"}
command1 | command2 [ | command3 ... ]
```

## Execution Mechanics

**Concurrency and Blocking**
Bash executes all commands within a pipeline concurrently. `command2` does not wait for `command1` to terminate before initializing. Instead, `command2` blocks on read operations until `command1` writes data into the pipe buffer. If the pipe buffer fills, `command1` blocks on write operations until `command2` consumes the data.

**Subshell Isolation and `lastpipe`**
By default, each command in a pipeline is executed in its own isolated subshell environment. Because of this architectural design, variable assignments, directory changes, or state modifications made within any segment of the pipeline do not propagate back to the parent shell.

```bash theme={"dark"}

# The variable 'VAR' is modified in a subshell and will not persist
echo "new_value" | read VAR

# $VAR remains empty or unchanged in the parent shell
```

Introduced in Bash 4.2, the `lastpipe` shell option overrides this default behavior for the final command in a pipeline. When `lastpipe` is enabled and job control is disabled (which is the default state in non-interactive scripts), the rightmost command executes in the current shell environment, allowing state changes to persist.

```bash theme={"dark"}
shopt -s lastpipe

# Job control must be disabled (set +m) for lastpipe to function in interactive shells
echo "new_value" | read VAR

# $VAR now contains "new_value" in the parent shell
```

## Exit Status and Error Handling

**Default Exit Status**
By default, the exit status (`$?`) of an entire pipeline is determined exclusively by the exit status of the rightmost command. If `command1` encounters a fatal error but `command2` executes successfully, the pipeline returns `0` (success).

**The `pipefail` Option**
To enforce strict error evaluation, Bash provides the `pipefail` option. When enabled, the pipeline's return status is the value of the rightmost command to exit with a non-zero status, or `0` if all commands exit successfully.

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

**The `PIPESTATUS` Array**
Regardless of the `pipefail` setting, Bash populates an internal array variable named `PIPESTATUS`. This array contains the individual exit status codes of every command in the most recently executed foreground pipeline, indexed from left to right.

```bash theme={"dark"}
command1 | command2 | command3

# ${PIPESTATUS[0]} holds the exit code of command1

# ${PIPESTATUS[1]} holds the exit code of command2

# ${PIPESTATUS[2]} holds the exit code of command3
```

## Standard Error (stderr) Routing

The standard `|` operator strictly routes file descriptor 1 (`stdout`). It does not capture or pipe file descriptor 2 (`stderr`). To pipe both `stdout` and `stderr` simultaneously, Bash 4.0 introduced the `|&` operator, which acts as syntactic sugar for redirecting `stderr` to `stdout` before piping.

```bash theme={"dark"}

# Pipes only stdout
command1 | command2


# Pipes both stdout and stderr (Bash 4.0+)
command1 |& command2


# POSIX-compliant equivalent to |&
command1 2>&1 | command2
```

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