Shell - Pipeline

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - Pipeline

A pipeline is a succession of command separated by a pipe operator. All command(s) in a pipeline are executed in parallel.


3 - Syntax

The format for a pipeline is

[time [-p]] [!] command1 [ [| or |&] command2 ...]


  • time print the time statistics.
  • The -p option changes the output format to that specified by posix. The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed.

The standard output of command is connected via a pipe to the standard input of command2. This connection is performed before any redirections specified by the command.

If the pipe operator ‘|&’ is used, the standard error of command1 is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error is performed after any redirections specified by the command.

4 - Execution

4.1 - (A)synchronous

If the pipeline is not executed asynchronously (see Lists), the shell waits for all commands in the pipeline to complete.

4.2 - Parallel

Each command in a pipeline is executed as a separate process (i.e., in its own subshell). The shell waits for all commands in the pipeline to terminate before returning a value.

4.3 - Exit Status

The exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled (see The Set Builtin).

If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.

If the reserved word ‘!’ precedes the pipeline, the exit status is the logical negation of the exit status.


5 - Documentation / Reference