help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] && precedence


From: DJ Mills
Subject: Re: [Help-bash] && precedence
Date: Wed, 9 Aug 2017 13:42:11 -0400

On Wed, Aug 9, 2017 at 10:35 AM, Russell Shaw <address@hidden>
wrote:

> On 09/08/17 22:48, Russell Shaw wrote:
>
>> 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.
>>
>
> Correction: "Simple command" should have been "Pipeline".
>
>
Why wouldn't you just use if statements anyway? Chains of && and || are
horrific for readability and troubleshooting


reply via email to

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