2013-12-20 53 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'踢了一些屁股,因爲它變成了 –