38

Apologies if this is a duplicate post. I did try searching to see if someone already asked/answered this but didn't find anything.

What does || mean in bash? For example, in researching steps to troubleshoot a driver issue, I came across this command:

modprobe -q vmxnet3 && echo "vmxnet3 installed" || echo "vmxnet3 not installed"

I get that the first part is querying modprobe and returning a response of "vmxnet3 installed" if it receives a successful return code... but... what's the last part doing?

Mike B
  • 8,900

3 Answers3

33

|| is the OR operator. It executes the command on the right only if the command on the left returned an error. See Confusing use of && and || operators.

I think your example isn't correct bash though. The part to the right of || is missing the echo command. (This was fixed.)

15

This is an "OR" condition.

For a simplified example lets look at

 cmd1 || cmd2

What happens is that if cmd1 succeeds (i.e. exit code 0), then cmd2 will NOT be performed. cmd2 will only be performed if cmd1 fails.

This is one reason why returning exit codes is so important.

BTW, this feature was present in very old UNIX shells as there were no IF statements in the old sh. This of course was before csh even existed.

mdpc
  • 6,834
15

Just like &&, || is a bash control operator:

  • && means execute the statement which follows only if the preceding statement executed successfully (returned exit code zero).

  • || means execute the statement which follows only if the preceding statement failed (returned a non-zero exit code).

Some of the other control operators are:

  • & means execute the preceding statement in the background.

  • ; means execute the preceding statement and, after it completes, proceed to the next statement.

  • | means execute the preceding statement and connect its stdout the to stdin of the statement which follows.

Combining && and ||

&& and || can be combined to make an alternative to if-then-else. Consider:

if some_command
then
    echo yes
else
    echo no
fi

The above is similar to:

some_command && echo yes || echo no

I said "similar" rather than identical because the two are different in the case that the echo yes command fails. If the echo yes command were to fail (exit with a non-zero return code) then the echo no command is executed. This would not happen with the if-then-else-fi version. The echo command will only fail, though, if there is a write-error on the terminal. Consequently, this difference rarely matters and the && and || form is often used as a short-cut.

John1024
  • 74,655
  • John, just a guess; you did not concisely answer the question. Extending the answer from logical || to && may be okay, or adding the (missing in your answer) ! negation, but extending to (a few selected of) other syntactical tokens may be considered off-topic. I wouldn't downvote, though (but in the presence of other fitting answers also not upvote). – Janis Mar 16 '15 at 19:31
  • 1
    Actually, I'd say the presence of other fitting answers can be an upvote. For instance, I was searching for pretty much an answer like this, and since this question is at the top of google's results, spot on! So, although this answer is off-topic to the specific question here, it's helpful for the summary in a broader context. – Bendemann Mar 14 '21 at 16:49