What does "|&" mean in Bash?
Asked Answered
C

4

98

For example

echo "aaa" |& cat

What does |& mean here?

Is there a website recommended to look up those? Since most search engines can't support searching special characters.

Coruscation answered 13/2, 2016 at 20:33 Comment(1)
FYI: echo "aaa" |& cat may lead to -bash: syntax error near unexpected token `&' if bash --version is GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14).Heedless
P
94

From man 1 bash, section Pipelines:

[time [-p]] [ ! ] command [ [|⎪|&] command2 ... ]

If |& is used, command's standard error, in addition to its standard output, is connected to command2's standard input through the pipe

So it is just like the pipe operator |, but piping both standard output and standard error.

Parable answered 13/2, 2016 at 20:36 Comment(2)
@BallpointBen exactly. From gnu.org/software/bash/manual/bash.pdf: 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 |.Cyclostyle
@Cyclostyle It's worth noting that |& isn't widely supported and you are better off using 2>&1 | for greater shell support across different platforms and different Linux distributions.Heuer
H
20

This operator pipes both standard output and standard error from the left hand side to the right hand side.

The Bash reference manual has a comprehensive index of all operators where you can look up these operators.

Hexapla answered 13/2, 2016 at 20:36 Comment(0)
D
6

If |& is used,

command_1’s standard error, in addition to it's standard output, is connected to command_2’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.

Dissipation answered 19/9, 2021 at 12:37 Comment(0)
R
2

command1 |& command2 equal command1 2>&1 | command2

Recaption answered 15/11, 2022 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.