我在命令行中使用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。
我不明白你为什么这样做重定向输出的两个级别,只是用'2>&1',它应该是在两个相同的...或者,是否有使用'1&2'的理由? –
这只是为了探索他们为什么表现不同。是的,这是一个玩具的例子。我并不是在问这个问题本身“完成某件事情”,而是要求它获得更好的理解,以便我能够理解和解释这是如何工作的,以便我可以在以后使用知识的力量完成许多事情。 –
并回答你的问题具体...'1>&2'应该送标准输出到标准错误(这是终端),但zsh中似乎仍然可以发送一个副本到管为好。 –