Search

12/16/2013

unix - In the shell, what is " 2>&1 "? - Stack Overflow

unix - In the shell, what is " 2>&1 "? - Stack Overflow

1 is stdout. 2 is stderr. Here is one way to remember this construct (altough it is not entirely accurate): at first, 2>1 may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1". & indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1.
What does “> /dev/null 2>&1″ mean? at Xaprb
wibble > /dev/null will redirect STDOUT to a BLACK HOLE wibble > /dev/null 2>&1 will redirect STDERR to STDOUT which in turn is redirected to a BLACK HOLE
孤島日誌: Unix 重新導向跟 2>&1
Note that the order of redirections is significant. For example, the command ls > dirlist 2>&1 directs both standard output and standard error to the file dirlist, while the command ls 2>&1 > dirlist directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist. 那麼倒過來為什麼不行?因為倒過來的話我們會先把 stderr 指向 stdout,但是當 stdout 被導向 dirlist 時會捨棄舊的 fd=1,開啟 dirlist 為新的 fd。這時 stderr 指向的卻還是舊的 stdout 的 fd,因此並不會寫入 dirlist。 2>&1 -> 把 stderr 指到 stdout 2>&1 > dirlist -> > dirlist 後, 變成指到 dirlist 這個檔案, 本來的 stderr 指到原本的 stdout 所以指不到

沒有留言: