help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] && precedence


From: John McKown
Subject: Re: [Help-bash] && precedence
Date: Wed, 9 Aug 2017 07:07:31 -0500

On Wed, Aug 9, 2017 at 6:35 AM, 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?
>
>
​What you are expecting can be coded like: true || { true && echo hi ; }

I think what you are expecting​ is like in arithmetic where || is like +
and && is like * so that the "true && echo hi" is a "single" operand to the
"||" operator. But I don't think that BASH does it that way. To expand a bit
the first "true" is run and $? is set to 0. The || is evaluated and says
"no, don't execute the next command because $? is 0", so the second "true"
is not run. The && is then evaluated and $? is still set to 0 from the
first true and so the "echo hi" is run.



-- 
Veni, Vidi, VISA: I came, I saw, I did a little shopping.

Maranatha! <><
John McKown


reply via email to

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