Shell – no null in /dev error

command lineshell

I executed the following line:

which lsb_release 2&>1 /dev/null

Output:

error: no null in /dev

When I verified the error using the ls /dev/null command, null was present in /dev. Why is this error occurring? I could not decipher the problem.

UPDATE

I just tried the above which command on someone else's system, it worked perfectly without generating the error that I got.

Best Answer

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
Related Question