Custom Search

Saturday, July 16, 2016

What is the meaning of /dev/null 2>&1

How to redirect both STDOUT and STDERROR to /dev/null

First, let me try without "2>&1"

a)
This command redirect output of command "git --version" to /dev/null
That is redirect only STDOUT to /dev/null 

$git --version >/dev/null
$

b)
This command does not have output to redirect to /dev/null. Because command
"gitxxx --version" failed with Error "gitxxx: command not found". We didn't
tell shell to redirect Error to /dev/null, So it printed error in STDOUT(1).

$gitxxx --version >/dev/null
gitxxx: command not found
$

c)
We can tell shell to redirect Error to STDOUT(1).
Here 2 means STDERROR and 1 is STDOUT.
2>&1 Means redirect STDERROR to STDOUT file descriptor.

So below command will redirect both STDOUT and STDERROR to /dev/null

$git --version >/dev/null 2>&1
$gitxxx --version >/dev/null 2>&1

No comments:

Post a Comment