zsh和bash之间的I / O重定向差异

时间:2013-12-20 04:12:19

标签: bash shell terminal zsh io-redirection

我在命令行使用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。

1 个答案:

答案 0 :(得分:2)

归因于zsh' mult_ios feature

zsh中,当fd重定向两次时,zsh会实现内部tee

ls > foo > bar

ls的输出发送到管道,zsh将其输出到foobar

管道会让人感到困惑。

ls > foo | cmd

将输出发送到foocmd

您可以使用以下方法将其停用:

setopt no_mult_ios