40

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?

4
  • 1
    See the Bash Guide entry on Control Operators...
    – jasonwryan
    Commented Mar 16, 2015 at 18:10
  • This is likely to be closed. Commented Mar 16, 2015 at 18:18
  • Just out of curiosity, why the down vote? I agree that it's a duplicate and will mark it as such. At the time though, I didn't know and I thought my question was clearly phrased and consistent with SE format/policies.
    – Mike B
    Commented Mar 16, 2015 at 18:23
  • @MikeB The question is perfectly formatted and such (not that I know the EXACT rules, but still), It was just because it was a duplicate. Commented Mar 16, 2015 at 19:29

3 Answers 3

36

|| 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.)

1
  • 1
    Good point. That's a typo on my side. I'll fix it.
    – Mike B
    Commented Mar 16, 2015 at 18:10
17

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.

2
  • 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
    Commented Mar 16, 2015 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
    Commented Mar 14, 2021 at 16:49
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.

Not the answer you're looking for? Browse other questions tagged .