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

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

Solution 1:

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.

Solution 2:

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