I often see commands like:
command1 |& command2
So what we are doing here is to redirect stderr to stdin, so that both of them "become" stdin to the next element in the pipe.
Or better described in the Bash Reference manual -> Pipelines:
A pipeline is a sequence of simple commands separated by one of the control operators ‘|’ or ‘|&’.
...
If
|&
is used, command1’s standard error, in addition to its standard output, is connected to command2’s standard input through the pipe; it is shorthand for2>&1 |
. This implicit redirection of the standard error to the standard output is performed after any redirections specified by the command.
However, I wonder: is this syntax usable in all Bash versions? That is, is it always interchangeable with 2>&1 |
? Can it also be used in any other shell (ksh, csh...)?
This feature is for Bash 4+ only:
|& (bash4)
Source
dd. The parser now understands `|&' as a synonym for `2>&1 |', which redirects the standard
error for a command through a pipe.
Source
Also note that the checkbashisms script will flag it as well:
'\s\|\&' => q<pipelining is not POSIX>,
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With