加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash与zsh中的重定向和管道行为

发布时间:2020-12-16 01:33:40 所属栏目:安全 来源:网络整理
导读:以下命令输出不同的结果,具体取决于它是在bash还是zsh中运行: ls -l x | wc -l 如果在非空目录中执行,bash总是给出0,而zsh给出正确数量的文件. x包含ls -l的输出,如预期的那样. 为什么它不能用于bash? 阅读zshmisc手册页中的 MULTIOS documentation.这是zs
以下命令输出不同的结果,具体取决于它是在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 and bar. 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读