6

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...)?

3
  • Your title and the question which you finally asked seem like two different questions to me tbh. I suggest to edit title
    – MohitC
    Commented Oct 17, 2015 at 23:48
  • @MohitChandak you are right. I started with the title and then investigated about the topic. By the time I had some things sorted out, other questions prompted. Will edit it, thanks.
    – fedorqui
    Commented Oct 17, 2015 at 23:50
  • if you want to know what is usable in other shells, you'd better to try out posix mode of shells, since standard is the only way to guarantee one thing will definitely work in another system
    – Jason Hu
    Commented Oct 18, 2015 at 0:15

1 Answer 1

4

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

1
  • 1
    Wow, great research Steven!
    – fedorqui
    Commented Oct 18, 2015 at 0:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.