以下命令输出不同的结果,具体取决于它是在bash还是zsh中运行:
ls -l > x | wc -l
如果在非空目录中执行,bash总是给出0,而zsh给出正确数量的文件. x包含ls -l的输出,如预期的那样.@H_403_4@
为什么它不能用于bash?@H_403_4@
阅读zshmisc手册页中的
MULTIOS documentation.这是zsh的一个特性,它使输出同时重定向到多个文件,它也可以是一个管道.
原文链接:https://www.f2er.com/bash/383332.html例如@H_403_4@
ls >a >b
来自zshmisc文档:@H_403_4@
If the user tries to open a file descriptor for writing more than once,the shell opens the file descriptor as a pipe to a process that copies its input to all the specified outputs,similar to tee,provided the MULTIOS option is set,as it is by default. Thus:@H_403_4@
date >foo >bar
writes the date to two files,named
foo
andbar
. Note that a pipe is an implicit redirection; thus@H_403_4@
date >foo | cat
writes the date to the file
foo
,and also pipes it to cat.@H_403_4@
要启用它,请执行setopt multios,关闭setopt nomultios:@H_403_4@
$setopt nomultios $ls -l > x | wc -l 0 $setopt multios $ls -l > x | wc -l 36