Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between "2>1 > /dev/null" and "2>&1 >/dev/null"?

Tags:

shell

I would like to know what the & means in the statement:

2>&1 > /dev/null

It's redirecting standard error to standard output and then to Bitbucket, but what's & in it?

Can I use it like the following?

2>1 >/dev/null
like image 707
kailash19 Avatar asked Feb 22 '12 06:02

kailash19


People also ask

What does >/ dev null 2 >& 1 mean?

It means that stderr ( 2 - containing error messages from the executed command or script) is redirected ( >& ) to stdout ( 1 - the output of the command) and that the latter is being redirected to /dev/null (the null device). This way you can suppress all messages that might be issued by the executed command.

What does 2 >& 1 mean and when is it typically used?

1 "Standard output" output file descriptor. The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").

What does null 2 mean?

/dev/null is a standard file that discards all you write to it, but reports that the write operation succeeded. 1 is standard output and 2 is standard error. 2>&1 redirects standard error to standard output.

What is &> dev Null in shell script?

/dev/null in Linux is a null device file. This will discard anything written to it, and will return EOF on reading. This is a command-line hack that acts as a vacuum, that sucks anything thrown to it.


1 Answers

The & means file descriptor1. So 2>&1 redirects standard error to whatever standard output currently points at, while 2>1 redirects standard error into a file called 1.

Also, the redirects happen in order. So if you say 2>&1 >/dev/null, it redirects standard error to point at what standard output currently points at (which is probably a noop), then redirects stdout to /dev/null. You probably want >/dev/null 2>&1.


1In the context of a file redirect -- when it is the next token immediately after a > or <. In other contexts it means something else.

like image 115
Chris Dodd Avatar answered Nov 29 '22 16:11

Chris Dodd