I am trying to understand a command like
[command] > /dev/null 2>&1
If I understand this correctly, the output of the command is redirected to /dev/null
and then the stderr
(file descriptor 2) is redirected to stdout
(file descriptor 1)
The end result is, only errors of [command]
will be output
To understand this, I executed the following commands:
bash-3.2$ echo "Hello World" 1>&2
Hello World
Here, I am echoing "Hello World" and redirecting it to stderr
.
And now I run the following:
bash-3.2$ echo "Hello World" 1>&2 2>&1
Hello World
I am taking the stderr
and redirecting it to stdout
. And as expected, it prints Hello World
Now I run this
bash-3.2$ echo "Hello World" 1>&2 1>&1
Hello World
What I am trying to do is to printout stdout
.
However, there is no stdout
since I redirected it to stderr
.
So why is it printing Hello World
?
I would have expected it to print nothing.