Chandu
Chandu

Reputation: 2049

Whats the difference between redirections "1>/dev/null 2>&1" and " 2>&1 1>/dev/null"?

Whats the difference between redirections 1>/dev/null 2>&1 and 2>&1 1>/dev/null

It seems 1st one displays output to stdout but not the second one.

Can somebody explain ! Thanks

Upvotes: 0

Views: 713

Answers (3)

hyena
hyena

Reputation: 338

OK. This is a really old post. See a related post here. I wanted to answer this comment in the other post "If & indicates a file descriptor then why is there no & before 2?" by user6708151, but my reputation is too low. I'll explain the way I interpret the shell redirection syntax. No one has mentioned it yet, not to my knowledge.

So, we have the syntax [x][operator][y], where x and y are file descriptors, and operator is a redirection operator. For example, [command] 1>/dev/stderr discards any output from [command]. Here, x is a file descriptor variable, and y a file descriptor value. An example will make this clear.

[command] 2>&1 means that the shell is putting the file descriptor value of variable 1 to variable 2, and in effect redirecting error output to stdout. The symbol & is nessesary here because it signifies that 1 is a variable not a file name instead.

To answer the orignial question, refer to the tables below. Note that the shell parse redirections from left to right, and you also need to read the table from left to right.

1>/dev/null 2>&1

Variable Default value 1>/dev/null 1>/dev/null 2>&1
stdin 0 0 0
stdout 1 /dev/null /dev/null
stderr 2 2 /dev/null

2>&1 1>/dev/null

Variable Default value 2>&1 2>&1 1>/dev/null
stdin 0 0 0
stdout 1 1 /dev/null
stderr 2 1 1

The last column is the end result for each redirection.

Upvotes: 1

Peder Klingenberg
Peder Klingenberg

Reputation: 41125

Expanding on Lokendra26's answer a bit: /dev/null is a special file on your system, a device for discarding anything written to it. It's common to send output there if you don't want to see it. "File" in this case, and unix terminology in general, can be both a normal disk file, or a device like the null device or your terminal.

The "1" and "2" are file descriptors, designators for places to send output. Programs use FD 1, "standard output", as the target for ordinary output, and FD 2, "standard error", for error output. These file descriptors can point to different files at different times. Normally they both point at your terminal, so you se output from your programs written there.

The & operator is more than just for disambiguation. It actually means "look up whatever this FD points to at this point".

It is important to understand these details in order to understand the difference between the two redirections you are asking about.

1>/dev/null 2>&1 this is actually two statements, processed in sequence. First, point "standard output" at the null device (thus discarding anything written to it). Second, point "standard error" at whatever "standard output" is pointing to, in this case /dev/null. The end result is that output from both file descriptors will be discarded.

2>&1 1>/dev/null is likewise two statements. First, point "standard error" at whatever "standard output" is pointing to. Normally this will be your terminal, as I wrote above. Second, point "standard output" at /dev/null. End result - only "standard output" is discarded, "standard error" will still print to your terminal.

Upvotes: 2

Lokendra
Lokendra

Reputation: 79

In Unix ">" stands for the redirection of the output to a file or elsewhere.

"1>" - Stands for output from stdout pipeline

"2>" - Stands for output from stderr (error) pipeline (Errors goes into this pipeline)

So from your question,

"1>/dev/null" : Tell the system to direct standard output to null(file) kept at /dev

"2>&1" : Tell the system to redirect the output of stderr pipeline to the place where the output of stdout pipeline is going. So In this case you get both the stdout and the stderr output written inside a single file.

While "2>&1 1>" this doesn't make sense to me as it would be equivalent to "2>&1"

PS. If you are confused about the &1 part then that is used to resolve the ambiguity which may occur when 1 is the name of a file.

Hope that makes sense to you.

Upvotes: 0

Related Questions