Order matters. The way to reason about redirections is to read them from left to right and realize that redirections make streams point at the same place. They don’t make streams point at each other.
What does that mean? If you say 2>&1 then you are redirecting stderr to wherever stdout is currently redirected to. If stdout is going to the console then stderr is, too. If stdout is going to a file then stderr is as well. If you follow this up by then redirecting stdout, stderr still points to what stdout used to point to. It does not “follow” stdout to the new location.
那是什么意思?如果你這么說,2>&1那么你正在將stderr重定向到stdout當前被重定向到的任何地方。如果標準輸出到控制臺,那么stderr也是。如果標準輸出到一個文件,那么stderr也是。如果您通過重定向標準輸出繼續執行此操作,stderr仍會指向stdout 用于指向的內容。它不會“跟隨”stdout到新的位置。
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/94068
補充: cmd 2>&1 >log 錯誤重定向通過正確重定向直接輸出至終端,正確重定向直接輸出至log文件
可以通過 (cmd 2>&1) >log 將錯誤重定向和正確重定向的內容全部輸出至文件