vertical-bar
Pipe output from one command to another
TLDR
Pipe stdout to stdin
Pipe both stdout and stderr to stdin
SYNOPSIS
command1 [options] [arguments] | command2 [options] [arguments]
DESCRIPTION
The vertical bar, |, is a powerful Linux shell operator known as the pipe. It serves as a conduit, redirecting the standard output (stdout) of the command on its left to become the standard input (stdin) of the command on its right. This allows for the creation of complex command pipelines, where the results of one program can be directly processed by another, without needing intermediate files.
This mechanism embodies the Unix philosophy of building small, sharp tools that do one thing well. By chaining these tools with pipes, users can perform sophisticated data manipulation, filtering, and transformation tasks. For instance, `ls -l | grep "\.txt" | wc -l` first lists directory contents, then filters for lines containing ".txt", and finally counts those lines. Pipes are fundamental for automation, scripting, and efficient command-line workflows.
CAVEATS
The pipe operator itself does not have command-line options or parameters. However, the commands connected by the pipe operator will have their own distinct sets of options and arguments. Each command in a pipeline runs as a separate process, which can introduce some overhead. If any command in the pipeline fails, the subsequent commands may receive unexpected or no input, potentially leading to errors. Error messages from individual commands are typically sent to standard error (stderr) and are not piped, appearing directly on the terminal.
STANDARD STREAMS
Understanding pipes requires knowledge of standard streams: stdin (standard input, typically keyboard), stdout (standard output, typically terminal screen), and stderr (standard error, also typically terminal screen). The pipe operator specifically redirects stdout of the left command to stdin of the right command. stderr is not piped and usually remains connected to the terminal.
UNIX PHILOSOPHY AND COMPOSABILITY
Pipes are a prime example of the Unix philosophy: "Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface." This promotes the creation of small, specialized tools that can be combined in countless ways to achieve complex tasks, fostering reusability and flexibility.
HISTORY
The concept of pipes was introduced by Douglas McIlroy at Bell Labs during the development of Unix in the early 1970s. It was a groundbreaking innovation that significantly contributed to the modularity and power of the Unix operating system. The pipe mechanism was inspired by McIlroy's observation that shell users frequently used temporary files to pass data between programs. The pipe provided a more elegant and efficient in-memory alternative, becoming a cornerstone of the Unix philosophy and design, allowing simple tools to be combined into powerful pipelines.