以下命令输出不同的结果,具体取决于它是在bash还是zsh中运行:
ls -l > x | wc -l
如果在非空目录中执行,bash总是给出0,而zsh给出正确数量的文件. x包含ls -l的输出,如预期的那样.
为什么它不能用于bash?
阅读zshmisc手册页中的
MULTIOS documentation.这是zsh的一个特性,它使输出同时重定向到多个文件,它也可以是一个管道.
例如
ls >a >b
将获得a和b两者填充目录的内容.
来自zshmisc文档:
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:
date >foo >bar
writes the date to two files,named
foo
andbar
. 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.
要启用它,请执行setopt multios,关闭setopt nomultios:
$setopt nomultios $ls -l > x | wc -l 0 $setopt multios $ls -l > x | wc -l 36