3

From this I understand that &> /dev/null in zsh redirects both stdout and stderr to /dev/null. Hence,

echo a &> /dev/null

gives no output.

However, when I do

echo a &> /dev/null | cat

cat will print the a, where I expected it to print nothing.

What is going on here?

4

As don_crissti already mentioned, this is the default behaviour of zsh and can be switched off with unsetopt multios.

Also see the manpage of zshmisc.

  • follow up question: why would you ever redirect all output to /dev/null before piping it to another program? – Ragnar Jan 7 '17 at 13:21
  • 3
    @Ragnar /dev/null is just a file as any other, and redirection to a file and at the same time to a terminal is very common practice. Zsh cannot guess what you are trying to achieve. – jimmij Jan 7 '17 at 13:28
  • 1
    @Ragnar: you probably can answer that question on your own. – Thomas Jan 7 '17 at 13:31
  • @Thomas, it seems to me that echo a &> /dev/null | cat is equivalent to echo a 2> /dev/null | cat (because we don't care about the a being sent to /dev/null anyway, right?), so why not use that instead – Ragnar Jan 7 '17 at 13:42
3

Note that a pipe is an implicit redirection; thus

date >foo | cat

writes the date to the file ‘foo’, and also pipes it to cat.

From here: http://zsh.sourceforge.net/Doc/Release/Redirection.html.

Thus, with multios option echo a &> /dev/null | cat is equivalent to echo a 2>1 | tee /dev/null | cat in other shell, like bash.

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