Redirection Operator in Linux

Have you used 2&>1

Rishab Batra
1 min readJun 24, 2022

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!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Rishab Batra
Rishab Batra

No responses yet

Write a response