Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can I use |& in Bash? Is it usable in other shells?

Tags:

bash

shell

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 for 2>&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...)?

like image 780
fedorqui 'SO stop harming' Avatar asked Oct 17 '15 23:10

fedorqui 'SO stop harming'


1 Answers

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

like image 114
Zombo Avatar answered Nov 15 '22 06:11

Zombo