How does AND and OR operators work in Bash?

I tried the following command on 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!


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

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.