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
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:
we could rewrite the command like this:
we don’t see any output in the screen. We changed the standard output (stdout) location to a file.
if we try to cat a file that doesn’t exist,
like this:
Using stderr file descriptor (2) to redirect the errors to a file
redirect both stdout and stderr to the same place.
'/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
Post a Comment