5

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.

6

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
  • I'm using bash and the same command is executing on another system and not on mine. And, the error that you've mentioned is a little different than what I'm encountering although the meaning is same. – Abhimanyu Saharan Jul 3 '16 at 13:32
  • Note 2&>1 is not 2>&1. In this case this will add 2 as a parameter, backgroup the run and then run >1 /dev/null. – Stephen Harris Jul 3 '16 at 13:33
  • I tried using 2>, I'm still getting the same error. That is why so confused why it is happening this way with me – Abhimanyu Saharan Jul 3 '16 at 13:34
  • So, then it must be a which issue. I'll figure out what my system is using and alter the code to match my system. – Abhimanyu Saharan Jul 3 '16 at 13:36
  • @StephenHarris Ooops..edited.. – heemayl Jul 3 '16 at 13:37
1

The syntax of the command which lsb_release 2&>1 /dev/null is wrong.

You probably meant to run which lsb_release 2> /dev/null assuming you're running an "sh" type shell.

If you're running csh type shell then you have more problems with stderr redirection.

  • I'm using sh type shell. I tried your solution too, same issue on my system. – Abhimanyu Saharan Jul 3 '16 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.