summaryrefslogtreecommitdiff
path: root/devdocs/bash/pipelines.html
blob: 31a97cdfe711f3f6c09d2af301817571ddc14fc0 (plain)
1
2
3
4
5
6
7
8
9
<h1 class="subsection">Pipelines</h1>   <p>A <code>pipeline</code> is a sequence of one or more commands separated by one of the control operators ‘<samp>|</samp>’ or ‘<samp>|&amp;</samp>’. </p>    <p>The format for a pipeline is </p>
<div class="example"> <pre class="example">[time [-p]] [!] command1 [ | or |&amp; command2 ] …
</pre>
</div> <p>The output of each command in the pipeline is connected via a pipe to the input of the next command. That is, each command reads the previous command’s output. This connection is performed before any redirections specified by <var>command1</var>. </p> <p>If ‘<samp>|&amp;</samp>’ is used, <var>command1</var>’s standard error, in addition to its standard output, is connected to <var>command2</var>’s standard input through the pipe; it is shorthand for <code>2&gt;&amp;1 |</code>. This implicit redirection of the standard error to the standard output is performed after any redirections specified by <var>command1</var>. </p> <p>The reserved word <code>time</code> causes timing statistics to be printed for the pipeline once it finishes. The statistics currently consist of elapsed (wall-clock) time and user and system time consumed by the command’s execution. The <samp>-p</samp> option changes the output format to that specified by <small>POSIX</small>. When the shell is in <small>POSIX</small> mode (see <a href="bash-posix-mode">Bash POSIX Mode</a>), it does not recognize <code>time</code> as a reserved word if the next token begins with a ‘<samp>-</samp>’. The <code>TIMEFORMAT</code> variable may be set to a format string that specifies how the timing information should be displayed. See <a href="bash-variables">Bash Variables</a>, for a description of the available formats. The use of <code>time</code> as a reserved word permits the timing of shell builtins, shell functions, and pipelines. An external <code>time</code> command cannot time these easily. </p> <p>When the shell is in <small>POSIX</small> mode (see <a href="bash-posix-mode">Bash POSIX Mode</a>), <code>time</code> may be followed by a newline. In this case, the shell displays the total user and system time consumed by the shell and its children. The <code>TIMEFORMAT</code> variable may be used to specify the format of the time information. </p> <p>If the pipeline is not executed asynchronously (see <a href="lists">Lists of Commands</a>), the shell waits for all commands in the pipeline to complete. </p> <p>Each command in a multi-command pipeline, where pipes are created, is executed in its own <em>subshell</em>, which is a separate process (see <a href="command-execution-environment">Command Execution Environment</a>). If the <code>lastpipe</code> option is enabled using the <code>shopt</code> builtin (see <a href="the-shopt-builtin">The Shopt Builtin</a>), the last element of a pipeline may be run by the shell process when job control is not active. </p> <p>The exit status of a pipeline is the exit status of the last command in the pipeline, unless the <code>pipefail</code> option is enabled (see <a href="the-set-builtin">The Set Builtin</a>). If <code>pipefail</code> 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 ‘<samp>!</samp>’ precedes the pipeline, the exit status is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value. </p><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br>
    <a href="https://www.gnu.org/software/bash/manual/html_node/Pipelines.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/Pipelines.html</a>
  </p>
</div>