我看到重定向從左到右處理。所以在這個例子中爲什麼重定向+管道(2>&1 |)合併兩個流而不是將stderr移動到stdout?
command 2>&1 | less
人們會認爲fd 2先被引導到fd 1,然後fd 1被髮送到管道。所以F1和2指向不同的地方。
但實際上這裏fd 1和2都指向管道,因爲由於某種原因fd 1先發送到管道然後fd 2發送到fd 1.爲什麼在這種情況下重定向從右到左處理?
我看到重定向從左到右處理。所以在這個例子中爲什麼重定向+管道(2>&1 |)合併兩個流而不是將stderr移動到stdout?
command 2>&1 | less
人們會認爲fd 2先被引導到fd 1,然後fd 1被髮送到管道。所以F1和2指向不同的地方。
但實際上這裏fd 1和2都指向管道,因爲由於某種原因fd 1先發送到管道然後fd 2發送到fd 1.爲什麼在這種情況下重定向從右到左處理?
管道不是重定向,因此實際上重定向(其中只有一個在您的示例中)正按照您的想法進行處理。管道是最後一個單獨的東西。
原因是管道與重定向不一樣。重定向影響一個命令,而管道連接兩個命令。
fd 2指向fd 1指向的位置(即stdout)。在
command 2>&1 | less
標準輸出已經在重定向生效之前指向管道!
如需更詳細的說明,請參見:
http://www.linuxtutorialblog.com/post/tutorial-the-best-tips-tricks-for-bash
# ...
# Well, here's a thing you should remember: bash reads command statements from
# the left to the right, but, before that, determines if there are multiple command
# statements and in which way they are separated. Therefore, bash already read
# and applied the "|" pipe symbol and stdout is already pointing to the pipe.
# ...