When can I use | & in bash? Can I use it in other shells?

I often see commands like:

command1 |& command2 

So what we are doing here is to redirect stderr to stdin, so that both of them will β€œbecome” stdin for the next item in the channel.

Or better described in the Bash Reference Guide β†’ Piping :

A conveyor is a sequence of simple commands separated by one of the control statements | or '| &.

...

If |& , a standard command line error, in addition to its standard output, connects to standard command2s inputs through pipes; this is a shorthand for 2>&1 | . This implicit redirection is a standard error for standard output is executed after any redirection specified by the command.

However, I am wondering: can this syntax be used in all versions of Bash? That is, is it always interchangeable with 2>&1 | ? Can it also be used in any other shell (ksh, csh ...)?

+6
source share
1 answer

This feature is for Bash 4+ only:

 |& (bash4) 

A source


 dd. The parser now understands `|&' as a synonym for `2>&1 |', which redirects the standard error for a command through a pipe. 

A source


Also note that script flags will also mark it:

 '\s\|\&' => q<pipelining is not POSIX>, 

A source

+4
source

All Articles