我在命令行使用zsh但是我编写的shell脚本运行bash以便它们是可移植的。
当我意识到这种差异时,我正在学习here的IO重定向:
请注意,该命令只是一个任意的命令,其第一行输出超过stderr,第二行超过stdout。
OS X上的 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
的输出顺序略有不同:
% 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
输出相同。
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
相同的结果。
我认为在深入研究bash
滴答之前我应该更喜欢从内到外学习zsh
的行为,但这不是很理想,因为机会至少是我期望的IO重定向的一半要做的事情将是我当然希望从zsh
提示尝试的时刻。而且我实际上非常投资于zsh
,因为我有大量的自定义插件,在这一点上做一个重大的努力就是做一个大的转换回bash。
答案 0 :(得分:2)
归因于zsh
' mult_ios feature。
在zsh
中,当fd重定向两次时,zsh
会实现内部tee
:
ls > foo > bar
将ls
的输出发送到管道,zsh
将其输出到foo
和bar
。
管道会让人感到困惑。
ls > foo | cmd
将输出发送到foo
和cmd
。
您可以使用以下方法将其停用:
setopt no_mult_ios