399

I would like a brief explanation of the following command line:

grep -i 'abc' content 2>/dev/null 
Ray Butterworth
  • 1,351
  • 2
  • 11
  • 29
Naive
  • 4,745
  • 11
  • 26
  • 35

5 Answers5

562

The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.

If you don't specify a number then the standard output stream is assumed, but you can also redirect errors:

> file redirects stdout to file
1> file redirects stdout to file

2> file redirects stderr to file

&> file redirects stdout and stderr to file
> file 2>&1 redirects stdout and stderr to file

/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.

Note that > file 2>&1 is an older syntax which still works, &> file is neater, but would not have worked on older systems.

ruohola
  • 105
  • 3
Warren Hill
  • 21,832
  • 28
  • 67
  • 88
  • 22
    is there a difference between `> /dev/null 2>&1` and `&> /dev/null` – Alexander Mills Oct 19 '17 at 00:25
  • 25
    In practice today I don't think there is `2>&1` is an older syntax so `&>` would not have worked years ago but both are equivalent. – Warren Hill Oct 19 '17 at 02:47
  • `foobar 2&1> file` does not work. It will send the `foobar 2` command to background and then run the command `1`, which will fail, it will also not redirect any output. You want `foobar > /dev/null 2&>1` or the shorter (and imo clearer): `foobar &> /dev/null`. – ruohola Oct 03 '20 at 12:08
  • `&>` appears to work in `bash` and `zsh` but not `sh`, `csh`, or `ksh`. (I know, I know, this is AU, not U&L, and this info is probably of little value.) – Mathieu K. Feb 11 '22 at 02:23
46

In short, it redirects stderr (fd 2) to the black hole (discards the output of the command).

Some commonly used pattern for redirection:

command > /dev/null 2>&1 &

Run command in the background, discard stdout and stderr

command >> /path/to/log 2>&1 &

Run command, append stdout and stderr to a log file.

In Bash 4+, a shorter (but less readable) form is functional

command &>> /path/to/log
Terry Wang
  • 9,505
  • 4
  • 37
  • 30
  • 6
    Is there a good reason to use `> /dev/null 2>&1` instead of `&> /dev/null`? – Craig McQueen Nov 30 '15 at 06:43
  • 9
    @CraigMcQueen `&>` is new in Bash 4, the former is just the traditional way, I am just so used to it (easy to remember). – Terry Wang Nov 30 '15 at 12:24
  • @CraigMcQueen according to a comment on [this answer](https://askubuntu.com/a/12110/638128), `&> /dev/null` may not work in some shells but > /dev/null 2>&1 will work in all POSIX compatible shells. – Stack Underflow Jan 06 '19 at 23:41
  • 4
    Why is it 2>&1 and not 2&1> ?? – marienbad Mar 11 '19 at 21:30
  • 1
    @marienbad that's actually a question worth posting :-) The syntax is `fn>target`, where `fn` is a file number (0-2 typically, some programs define more numbers) and `target` is usually a file name but here it is another filenumber - prefixed with `&` which is the syntax for "I want a filenumber instead of a file name". – toolforger Jun 07 '19 at 04:12
20

/dev/null is treated as black hole in Linux/Unix, so you can put anything into this but you will not be able to get it back from /dev/null.

Further, 2> means that you are redirecting (i.e. >) the stderr (i.e. 2) into the black hole (i.e. /dev/null)

Your command is:

grep -i 'abc' content 2>/dev/null 

Don't try to end with another forward slash like this - 2>/dev/null/ (it's not a directory).

Zanna
  • 69,223
  • 56
  • 216
  • 327
Indrajeet Gour
  • 301
  • 2
  • 6
12

grep -i 'abc' content will generate output which is displayed on your console, including any errors.

Specifying 2>/dev/null will filter out the errors so that they will not be output to your console.

In more detail: 2 represents the error descriptor, which is where errors are written to. By default they are printed out on the console.

\> redirects output to the specified place, in this case /dev/null

/dev/null is the standard Linux device where you send output that you want ignored.

Rot-man
  • 165
  • 1
  • 7
Mauro Zallocco
  • 121
  • 1
  • 2
5

First we need to talk about > operator. It redirect the output of left of symbol to right of symbol.

So it must thought as :

source_command > target_file

Other things that we must know

0 means stdin 
1 means stdout(useful output)
2 means stderr(error message output)

As default, it works as command 1 > target_file

As to /dev/null --> it is a special file that discards channel output redirect to it.

So in your question it means

Run the command and do not show me the error messages, discard them.

berkancetin
  • 248
  • 2
  • 7
  • 2
    It was very useful to have the meanings of different descriptors to get some more context and saved me an additional Google search. – maulik13 Dec 14 '20 at 10:05