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.

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

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.

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

# Pipes only stdout
command1 | command2


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


# POSIX-compliant equivalent to |&
command1 2>&1 | command2
Master Bash with Deep Grasping Methodology!Learn More