There are two streams of output from a process that are generally sent to the terminal: standard output (which is bound to file descriptor 1), and standard error (which is bound to file descriptor 2). This allows for easily separated capture of expected output, and error messages or other diagnostic information that is not generally expected to be output.
When you use the redirect (>
), that only kicks standard output to the specified file or location, leaving standard error untouched. This allows you to see if there are errors (like those you are seeing).
If you want to send all output, including standard error, you need to redirect both streams:
/path/to/program arg1 arg2 > /dev/null 2>&1
Or, alternatively but more explicitly:
/path/to/program arg1 arg2 > /dev/null 2> /dev/null
The syntax 2>&1
means "Send output currently going to file descriptor 2 to the same place that output going to file descriptor 1 is going to". >
is omitting the default of FD1, so it is semantically the same as 1>
, which might make 2>
make more sense.
First of all, redirections can occur anywhere in the command line, not necessarily at the end or start.
For example:
echo foo >spamegg bar
will save foo bar
in the file spamegg
.
Also, there are two versions of which
, one is shell builtin and the other is external executable (comes with debianutils
in Debian).
In your command:
which lsb_release 2>&1 /dev/null
by 2&>1
, you are redirecting the STDERR (FD 2) to where STDOUT is (FD 1) pointed at, not to /dev/null
and this is done first.
So the remaining command is:
which lsb_release /dev/null
As there is no command like /dev/null
, hence the error.
Note that, this behavior depends on whether which
is a shell builtin or external executable, bash
, ksh
, dash
do not have a builtin and use external which
and that simply ignores the error, does not show any error message.
On the other hand, zsh
uses a builtin which
and shows:
/dev/null not found
So presumably, that specific error is shown by the builtin which
of the shell you are using.
Also, it seems you wanted to just redirect the STDERR to /dev/null
if lsb_release
does not exist in the PATH
i.e. which
shows an error. If so, just redirect the STDERR to /dev/null
:
which lsb_release 2> /dev/null
Best Answer
< /dev/null
is used to instantly send EOF to the program, so that it doesn't wait for input (/dev/null
, the null device, is a special file that discards all data written to it, but reports that the write operation succeeded, and provides no data to any process that reads from it, yielding EOF immediately).&
is a special type of command separator used to background the preceding process.Without knowing the program being called, I do not directly know why it is required to run it in this way.