Search code examples
linuxmacosshell

What does the AND operator '&' do in "& tee"?


In my development environment there exists an alias that writes stdout to a specific file.

It uses pipe with tee, e.g., du -h | & tee /tmp/diskUsage.

I understand that AND operator makes whatever it precedes a file descriptor instead of filename, say by 2>&1 we are redirecting stderr to stdout instead of a file called "1" in current dir.

However, for the usage | & tee, there is nothing meaningful after '&' and before "tee", what does it do in this case?


Solution

  • |& is shorthand for "pipe both stderr and stdout from the left hand program to the right hand program".

    Basically, tee will receive both normal output and error output muxed together, not just stdout as plain | would do normally.

    Or to quote GNU docs:

    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 command1.