“管道标准错误,而不是标准输出"不在 Zsh 中工作 [英] "Pipe stderr, and not stdout" not working in Zsh

查看:31
本文介绍了“管道标准错误,而不是标准输出"不在 Zsh 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过管道传输 stderr 而不是 stdout? 完美捕获我的问题,第一个答案正是我最初尝试解决它的方式:

How to pipe stderr, and not stdout? perfecly captures my problem, and the first answer is exactly how I initially tried to solve it:

(echo stdout; echo 1>&2 stderr) 2>&1 >/dev/null | less

(带有 echo 的 subshel​​l 命令是一个最小的占位符,感谢 user1934428 演示了这个问题;我的实际问题有一个更有用的命令,但其他人都无法运行.)

(The subshell command with echos is a minimal placeholder thanks to user1934428 to demonstrate the issue; my actual problem has a more useful command, but one that everyone else can't run.)

但是,它不起作用:它同时显示标准输出标准错误.如果我移除管道,它会按预期工作,只显示 stderr.最终我意识到这可能是一个 shell 问题,并尝试了 bash:它在那里工作得很好.我正在使用 Zsh:Zsh 会导致此失败吗?

However, it doesn't work: It's showing both stdout and stderr together. If I remove the pipe, it works as expected, only showing stderr. Eventually I realized this might be a shell thing, and tried bash: It worked perfectly there. I'm using Zsh: what about Zsh makes this fail?

推荐答案

这是由于 MULTIOS,当您提供多个重定向时,它会复制输出流.管道是隐式重定向.

This is due to MULTIOS, which duplicates the output streams when you supply multiple redirections. Pipes are implicit redirections.

输入 ls >/dev/null |less 在您的 zsh 中,即使您重定向了它,您也会看到输出.这是 multios 复制流.

Type ls >/dev/null | less in your zsh and you will see output even though you redirected it. That's multios duplicating the stream.

unsetopt multios 将提供所需的行为.如果我不会总是忘记它直到它激怒我,这是一个非常方便的功能.

unsetopt multios will provide the desired behaviour. This is quite a handy feature if I wouldn't always forget about it until it irritates me.

http://zsh.sourceforge.net/Doc/Release/Redirection.html#Multios

这篇关于“管道标准错误,而不是标准输出"不在 Zsh 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆