This question already has an answer here:

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?

marked as duplicate by Community Mar 16 '15 at 18:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • See the Bash Guide entry on Control Operators... – jasonwryan Mar 16 '15 at 18:10
  • This is likely to be closed. – DisplayName Mar 16 '15 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 Mar 16 '15 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. – DisplayName Mar 16 '15 at 19:29

|| is the OR operator. It executes the command on the right only if the command on the left returned an error. See https://unix.stackexchange.com/a/24685/23305.

I think your example isn't correct bash though. The part to the right of || is missing the echo command:

> true && echo "worked" || echo "didn't work"
> false && echo "worked" || echo "didn't work"
didn't work
> false && echo "worked" || "didn't work"
bash: didn't work: command not found
  • 1
    Good point. That's a typo on my side. I'll fix it. – Mike B Mar 16 '15 at 18:10

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.


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
    echo yes
    echo no

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.

  • 1
    Any suggestions for why this answer attracted a downvote? – John1024 Mar 16 '15 at 18:19
  • 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

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