What does '>/dev/null 2>&1' mean in SHELL?

'>' is for redirection

'/dev/null' is a black hole where any data sent, will be discarded

'2' is the file descriptor for Standard Error

'&' is the symbol for file descriptor (without it, the following 1 would be considered a filename)

'1' is the file descriptor for Standard Out


$ cat foo.txt
foo
bar
baz

redirection is the mechanism used to send the output of a command to another place.
Here, for example, we are redirecting it to a file called out.txt:


$ cat foo.txt > out.txt

we could rewrite the command like this:

$ cat foo.txt 1> out.txt

we don’t see any output in the screen. We changed the standard output (stdout) location to a file.


$ cat out.txt
foo
bar
baz


if we try to cat a file that doesn’t exist,
like this:

$ cat nop.txt > out.txt
cat: nop.txt: No such file or directory
we see the error output in the screen, because we are redirecting just the standard output, not the standard error.

Using stderr file descriptor (2) to redirect the errors to a file

$ cat nop.txt 2> error.txt

$ cat error.txt
cat: nop.txt: No such file or directory

redirect both stdout and stderr to the same place.

$ cat foo.txt > out.txt 2>&1

$ cat out.txt
foo
bar
baz

$ cat nop.txt > out.txt 2>&1

$ cat out.txt
cat: nop.txt: No such file or directory

Comments

Popular posts from this blog

Python3 + Django + uWSGI + Nginx On FreeBSD

arduino最小構成 (atmega328/8MHz/3.3V/内部クロック)FT232RLにてブートローダーの書き込み

FreeBSD: Configuring Apache to permit CGI