23

I tried the following command in bash

echo this || echo that && echo other

This gives the output

this
other

I didn't understand that!

My dry run goes this way :

  1. echo this || echo that && echo other implies true || true && true
  2. Since, && has more precedence than ||, the second expression evaluates first
  3. Since, both are true, the || is evaluated which also gives true.
  4. Hence, I conclude the output to be:

that

other

this

Being from a Java background where && has more precedence than ||, I am not able to relate this to bash.

Any inputs would be very helpful!

1
  • note both like same precedence to left to right associative Commented Feb 12, 2013 at 16:20

6 Answers 6

24

From man bash

3.2.3 Lists of Commands

A list is a sequence of one or more pipelines separated by one of the operators ‘;’, ‘&’, ‘&&’, or ‘||’, and optionally terminated by one of ‘;’, ‘&’, or a newline.

Of these list operators, ‘&&’ and ‘||’ have equal precedence, followed by ‘;’ and ‘&’, which have equal precedence.

So, your example

echo this || echo that && echo other

could be read like

(this || that) && other
Sign up to request clarification or add additional context in comments.

Comments

8

In bash, && and || have equal precendence and associate to the left. See Section 3.2.3 in the manual for details.

So, your example is parsed as

$ (echo this || echo that) && echo other

And thus only the left-hand side of the or runs, since that succeeds the right-hand side doesn't need to run.

Comments

4

Boolean evaluation in bash is short-circuit: true || false will never evaluate the false operand, because the true operand is enough to determine the outcome of the operation. Likewise, false && true will not evaluate the true operand, because it cannot change the value of the expression.

Boolean evaluation in bash is actually used mainly for controlling the conditional evaluation of the operands, not their order. Typical usage is do_foo || do_bar_if_foo_fails or do_foo && do_bar_only_if_foo_has_succeeded.

In no situation will your echo that command be executed, because the echo this is true and determines the value of the entire echo this || echo that sub-expression.

Comments

3

From man bash:

Of these list operators, && and || have equal precedence, followed by ; and &, which have equal precedence.

So your output is expected.

Comments

2

I think you've already figured it out. Operator precedence doesn't work like that in bash. Everything just goes left to right in your example.

Comments

2

Let's explain what this does:

echo this || echo that && echo other

echo this || echo that -> echo that only if echo this FAILS.

&& echo other -> echo other only if the command before && SUCCEEDS.

So basically:

echo this---> SUCCESS ----> echo that ----> is not executed since echo this succeeded ---> echo other ---> is executed cause echo this || echo that was executed correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.