问题描述
我在命令行上使用zsh,但是我编写的shell脚本运行bash,以便它们可移植.
I use zsh at the command-line but the shell scripts I write run bash so that they are portable.
当我意识到这种差异时,我正在此处了解有关IO重定向的信息:
I was learning about IO redirection from here when I realized this difference:
请注意,该命令只是任意命令,其第一行输出在stderr之上,第二行输出在stdout之上.
Note that the command is just an arbitrary one whose first line of output is over stderr and the second line comes over stdout.
zsh
:
% ls -ld /tmp /tnt 1>&2 2>&1 | sed -e 's/^/++/'
ls: /tnt: No such file or directory
++ls: /tnt: No such file or directory
lrwxr-xr-x@ 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp
++lrwxr-xr-x@ 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp
bash
:
bash-3.2$ ls -ld /tmp /tnt 1>&2 2>&1 | sed -e 's/^/++/'
ls: /tnt: No such file or directory
lrwxr-xr-x@ 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp
我很难弄清zsh
的输出.
此外,在Linux上,zsh
的输出顺序略有不同:
Also, on a Linux the output order's slightly different for zsh
:
% ls -ld /tmp /tnt 1>&2 2>&1 | sed -e 's/^/++/'
ls: cannot access /tnt: No such file or directory
drwxrwxrwt. 13 root root 4096 Dec 19 23:11 /tmp
++ls: cannot access /tnt: No such file or directory
++drwxrwxrwt. 13 root root 4096 Dec 19 23:11 /tmp
但是
bash
的输出是相同的.
bash
output is identical though.
zsh
中的更多实验:
% ls -ld /tmp /tnt 1>&2 | sed -e 's/^/++/'
ls: /tnt: No such file or directory
lrwxr-xr-x@ 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp
++lrwxr-xr-x@ 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp
% ls -ld /tmp /tnt 2>&1 | sed -e 's/^/++/'
++ls: /tnt: No such file or directory
++lrwxr-xr-x@ 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp
那里的最后一个产生与bash
相同的结果.
That last one there produces identical results to bash
.
我认为我更喜欢在研究zsh
滴答声之前从内而外地学习bash
的行为,但这并不是很理想,因为机会至少是我期望做的IO重定向的一半我肯定会想从zsh
提示符尝试尝试一下.而且我实际上在zsh
上投入了很多钱,因为我有大量的自定义插件,这时要大力改回bash将是一项巨大的努力.
I figure I should prefer learning bash
's behavior inside-out before delving into how zsh
ticks, but this isn't really ideal because chances are at least half of the IO redirection I expect to be doing will be spur-of-the-moment hacks that I surely will want to try from the zsh
prompt. And I'm actually pretty invested in zsh
because I have a ton of custom plugins and it would be a major effort at this point to do a big switch back to bash.
推荐答案
这是由于zsh
的 mult_ios功能.
在zsh
中,当fd重定向两次时,zsh
实现内部的tee
:
In zsh
, when a fd is redirected twice, zsh
implements an internal tee
:
ls > foo > bar
将ls
的输出发送到管道,并且zsh
将其输入到foo
和bar
.
Sends the output of ls
to a pipe and zsh
feeds it to both foo
and bar
.
它可能会与管道混淆.
ls > foo | cmd
将输出发送到foo
和cmd
.
您可以通过以下方式禁用它:
You can disable it with:
setopt no_mult_ios
这篇关于zsh和bash之间的I/O重定向差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!