2013-12-20 134 views
0

我在命令行中使用zsh,但是我编写的shell脚本运行bash以便它们可以移植。zsh和bash之间的I/O重定向区别

我了解IO重定向从here当我意识到这种差异:

注意,命令只是一个任意的输出的第一行是在标准错误,第二行过来标准输出。

zsh在OS X:

% 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 
[email protected] 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp 
[email protected] 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 
[email protected] 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 
[email protected] 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp 
[email protected] 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 
[email protected] 1 root wheel 11 Oct 19 2012 /tmp -> private/tmp 

这最后一个有产生相同的结果,以bash

我想我应该更喜欢学习bash的行为,然后再深入研究zsh的滴答问题,但这不是很理想,因为机会至少有一半是我希望做的IO重定向,我当然会想要从zsh提示中尝试。我实际上很投资于zsh,因为我有大量的自定义插件,在这一点上做一个重大的努力来做一个大转换回bash。

+0

我不明白你为什么这样做重定向输出的两个级别,只是用'2>&1',它应该是在两个相同的...或者,是否有使用'1&2'的理由? –

+0

这只是为了探索他们为什么表现不同。是的,这是一个玩具的例子。我并不是在问这个问题本身“完成某件事情”,而是要求它获得更好的理解,以便我能够理解和解释这是如何工作的,以便我可以在以后使用知识的力量完成许多事情。 –

+0

并回答你的问题具体...'1>&2'应该送标准输出到标准错误(这是终端),但zsh中似乎仍然可以发送一个副本到管为好。 –

回答

1

这是由于zshmult_ios feature

zsh,当fd被重定向两次,zsh实现了一个内部tee

ls > foo > bar 

发送的ls输出到一个管道和zsh其馈送到两个foobar

它可能会与管道混淆。

ls > foo | cmd 

将输出发送到既foocmd

你可以禁用它:

setopt no_mult_ios 
+0

太棒了,'zsh'踢了一些屁股,因为它变成了 –