September 28, 2010

“ 2>&1 ”?

| |

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.

  • 2 is the default file descriptor for stderr.
  • 1 is the default file descriptor for stdout.
  • >& is shell syntax for "fold a file descriptor into another"

0 意見: