以下命令输出不同的结果,具体取决于它是在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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。