vertical-bar
Pipe output from one command to another
TLDR
Pipe stdout to stdin
Pipe both stdout and stderr to stdin
SYNOPSIS
command1 | command2 | command3 ...
DESCRIPTION
The vertical bar, commonly known as the pipe (|), is a fundamental command in Linux and other Unix-like operating systems. It allows you to connect the standard output of one command to the standard input of another. This enables you to chain commands together, creating powerful workflows where the output of one program becomes the input for the next. This facilitates data transformation, filtering, and analysis.
The pipe symbol redirects the stdout of the command on its left to the stdin of the command on its right. This can be chained indefinitely with multiple commands to create complex data processing pipelines. It's a crucial tool for scripting and command-line interaction, making complex tasks manageable by breaking them down into smaller, composable steps. Pipes are commonly used with commands like `grep`, `sort`, `awk`, `sed`, and many others to filter, transform, and analyze data.
CAVEATS
The pipe only redirects standard output. Standard error is not redirected and will still be displayed on the terminal unless explicitly redirected (e.g., `2>&1`). The pipe can create performance overhead for commands where execution time and resource consumption are an issue.
REDIRECTION OF STANDARD ERROR
To redirect both standard output and standard error through a pipe, use `2>&1` to merge standard error into standard output before piping. For example: `command 2>&1 | another_command`.
EXAMPLE USAGE
A common example is to search for a specific string within a file and then count the number of lines that contain that string: `grep 'pattern' file.txt | wc -l`. Here, `grep` filters the file, and `wc -l` counts the resulting lines.
IMPORTANT CONSIDERATIONS
The left side of the pipe must finish execution and give stdout for the right side to start execution. This could lead to possible bottlenecks if left side execution time is longer than right.
HISTORY
The pipe concept was introduced in Unix by Doug McIlroy and is a core element of the Unix philosophy of building simple, modular tools that can be combined to perform complex tasks. It's been a cornerstone of Unix and Linux ever since its inception, enabling powerful command-line scripting and data manipulation.