Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does redirection in pipelines behave differently in Bash and Zsh?

Redirection behaves differently in Bash and Zsh when hooked up to a pipe:

bash> echo foo >/dev/null | cat
bash>
zsh> echo foo >/dev/null | cat
foo
zsh>

Bash does what I expect; in Zsh, the STDOUT redirected to /dev/null seems to come back from the dead.

Why this difference? What's going on in each case?

like image 840
Sasgorilla Avatar asked Mar 02 '23 14:03

Sasgorilla


1 Answers

Zsh has a fancy feature called multios. If you turn it off, the behavior will be almost identical to Bash.

% echo a >/dev/null | cat
a
% set +o multios          
% echo a >/dev/null | cat
% 

And this is documented in the manual as follows.

Note that a pipe is an implicit redirection; thus[, when multios is on, and it is on by default]

date >foo | cat

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

like image 75
oguz ismail Avatar answered Apr 26 '23 04:04

oguz ismail