help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] && precedence


From: Russell Shaw
Subject: Re: [Help-bash] && precedence
Date: Wed, 9 Aug 2017 22:48:29 +1000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.1.0

On 09/08/17 22:10, Pierre Gaston wrote:
On Wed, Aug 9, 2017 at 2:35 PM, Russell Shaw <address@hidden> wrote:

Hi,
When i do:

   true || echo hi

there is no "hi" as expected


when i do:

   true || true && echo hi

i get "hi" echoed. Why does "true && echo hi" get evaluated?


The manual says:
"Of these list operators, && and ││ have equal precedence"

Take care that you also have && and || inside [[  ]] where they do not

From the horses mouth:

<http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html>

2.9.3 Lists

An AND-OR list is a sequence of one or more pipelines separated by the operators "&&" and "||" .

A list is a sequence of one or more AND-OR lists separated by the operators ';' and '&' and optionally terminated by ';', '&', or <newline>.

The operators "&&" and "||" shall have equal precedence and shall be evaluated with left associativity. For example, both of the following commands write solely bar to standard output:

    false && echo foo || echo bar
    true || echo foo && echo bar

--------------

I think what happens is this.

With equal precedence and left associativity, the parser shifts in a Simple command and evaluates it only if it is the first command, or the previous op was || and the "running" shell status is "fail", or if the previous op was && and the "running" shell status is "success". It shifts all the and-or list Simple commands through to the end.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]