I would like a brief explanation of the following command line:
grep -i 'abc' content 2>/dev/null
I would like a brief explanation of the following command line:
grep -i 'abc' content 2>/dev/null
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 file1> file
redirects stdout to file2> 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.In short, it redirects
stderr
(fd
2) to the black hole (discards the output of the command).Some commonly used pattern for redirection:
Run
command
in the background, discardstdout
andstderr
Run
command
, appendstdout
andstderr
to a log file.In Bash 4+, a shorter (but less readable) form is functional
/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:
Don't try to end with another forward slash like this -
2>/dev/null/
(it's not a directory).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.First we need to talk about
>
operator. It redirect the output of left of symbol to right of symbol.So it must thought as :
Other things that we must know
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