0

I have seen

2>&1

at the end of a bash command. What does this mean? Is it some kind of output redirection?

Thanks!

Mr Mikkél
  • 2,577
  • 4
  • 34
  • 52

2 Answers2

3

Short: It redirects all output made on STDERR to STDOUT.

> is a redirection operator which will - in the simplest form - redirect all output on STDOUT into a file.

test.sh > file

If you prefix > with a number it uses the output from this specific file descriptor - 2 in your example. 1 is stdout, 2 is stderr.

test.sh 2> file.err

will redirect all output from descriptor 2 = stderr to the file.

If you use the special notation &1 instead of a filename, the output is not written to a new file, but instead to the file descriptor with the given number, in this case 1. So:

test.sh 2>&1

redirects from file descriptor 2 (stderr) to file descriptor 1 (stdout)

It's useful if you want to collect all output regardless of where it happened (stdout or stderr) to further processing, like piping into another program.

Karsten S.
  • 2,349
  • 16
  • 32
0

1 is stdout. 2 is stderr.

2>&1 simply points everything sent to stderr, to stdout instead.

You will get more info about this : here

jaypal singh
  • 74,723
  • 23
  • 102
  • 147