Redirection Operator in Linux
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.
>& as redirect merger operator.
File descriptor 1 is the standard output (
File descriptor 2 is the standard error (
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
stdout. However, it will actually be interpreted as "redirect
stderr to a file named
& indicates that what follows and precedes is a file descriptor and not a filename. So the construct becomes:
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_fileand 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.