5
"command "> /dev/null 2>&1 || true  

Now I get that we are sending the output of a command we run, to a null file. I need to know specific meaning for *2>&1 || true*

12

TL;DR: >/dev/null 2>&1 || true effectively silences the command, throwing away any (error or normal) output and the result of the command, but not any of its side effects. What follows is an explanation of why.


Linux programs by default receive three file descriptors which are opened before the main code of the program begins executing. These are stdin (standard input, file descriptor 0), stdout (standard output, file descriptor 1) and stderr (standard error, file descriptor 2). These are normally tied to the current terminal, which today effectively means the keyboard, screen, and screen, respectively (but such was not always the case).

By specifying > /dev/null in the command (the redirection can generally go anywhere within the command, but by convention it goes at the end or very rarely at the beginning), you specify that the standard output (implicit 1, but can be specified using 1> instead of just >) should be redirected to write to /dev/null, which discards everything that is written to it.

2>&1 then redirects file descriptor 2 (2>) to what file descriptor 1 is currently bound to (&1).

A shortcut for when you want to redirect both stdout and stderr to the same place is to use &> as in &> /dev/null. This is not supported in all shells, however.

So by specifying > /dev/null 2>&1 you tell the system that you want to redirect the command's standard output into /dev/null, then redirect standard error into standard output (which in turn is redirected into /dev/null). This has the effect of silencing the command by throwing away all of its output that is sent to either stdout or stderr.

Note that some programs use alternative means of drawing to the screen, such as a terminal library like ncurses, and generally won't be affected by this or will be affected in unexpected ways. But for programs written to be used in a pipe fashion (which includes most programs that output simple text to the terminal), this works.

The || in turn means that if the program returns an exit status other than 0 (where by convention 0 means successful execution), then the shell will invoke the command on the other side of the ||, which in this case is true. Do not confuse this with the ordinary pipe, which uses a single | and ties the left-hand stdout to the right-hand stdin. The opposite of || is && which executes the right-hand side only if the left-hand side returned an exit status of 0, and a complementary is ; which simply separates multiple commands (the second command will be executed regardless of the exit status of the first command).

true, in its turn, is a very simple program that has as its only purpose to exit with a successful status. It isn't generally very useful by itself, but can be very useful for example when writing conditionals in shell scripts. Its opposite is false which exits with a failure (non-zero) status but otherwise, like true, does nothing.

So by appending || true after a command, you know that either way, something will have executed successfully by the time that set of commands has finished executing. Thus you know that the current status code (available in $?) will always indicate successful execution.

3

What does "command " > /dev/null 2>&1 || true mean in Linux?

2>&1 means "redirect STDERR(2) to STDOUT(1)"
(STDERR (Standard Error) has predefined file descriptor equal 2
and STDOUT's (Standard output) file descriptor is equal 1)

> - is the same as 1> and means redirect STDOUT to something

Then the whole operation will be translated as:

command > /dev/null 2>&1 - redirect STDOUT and STDERR file descriptors to the "black hole" or basically ignore all outputs of command
("black hole" or /dev/null is special device that accepts input and internally ignore it)

Regarding || true - If command return non 0 (zero) exit status (that usually indicating some type of error/failure) then true forcible set exit status code to 0 (zero) indicating success.

  • specifically, 2>&1 means "redirect STDERR (2) to STDOUT (1)". – quixotic Feb 17 '17 at 12:29
  • @quixotic Thanks ! Added your note to the answer. – Alex Feb 17 '17 at 12:47

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.