Redirection Operator in Linux

Have you used 2&>1

So this is used like below:

Running any test or starting any service, obviously want to store logs somewhere.

Let us say you started your service

./startmyservice.sh 2>&1 tee startmyservicelogs.log

I often forget this one, I am documenting it from stack overflow for my personal use.

Consider >& as redirect merger operator.

File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although 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 and precedes is a file descriptor and not a filename. So the construct becomes: 2>&1.

Note on this

2>1 may look like Redirect something to file named 1

2>&1 & will signify 1 is also a file descriptor here.

This is going to redirect standard error to standard output and standard output to some_file and print it to standard output also.

And tee command reads the standard input and writes it to both the standard output and one or more files mentioned.

We could say merge stderr to the file.

Source from stack overflow.

Thank You!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store