discuss: Thread: Incorrect AND / OR Precedence


[<<] [<] Page 1 of 1 [>] [>>]
Subject: Incorrect AND / OR Precedence
From: "Bulgrien, Dennis" ####@####.####
Date: 16 Sep 2021 15:42:25 +0100
Message-Id: <7b8d79db4a654128beb5ba16b196656f@astronics.com>

Please double-check https://tldp.org/LDP/abs/html/opprecedence.html which states that && AND has higher precedence that || OR. Other web sites and my experience indicate your documentation is incorrect. In the example below, if && was executed first then echo would have to run to complete the expression prior to running false, but in actuality echo does not run because || runs first and short-circuit condition says there is no point in running echo.

$ false || false && echo y

This E-mail is confidential. It may also be legally privileged. If you are not the addressee you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return E-mail.

Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions.
PLEASE NOTE that Freedom Communication Technologies is now a part of Astronics Test Systems (ATS) and email addresses are changing. Please update my contact info by replacing @freedomcte.com with @astronics.com to direct your messages to my new email address (for example, ####@####.#### will be updated to ####@####.####
Subject: Re: Incorrect AND / OR Precedence
From: "Ian! D. Allen" ####@####.####
Date: 9 Dec 2021 08:59:32 +0000
Message-Id: <YbHFbF/mFheXXj5q@idallen-oak.home.idallen.ca>

On Thu, Sep 16, 2021 at 10:41:49AM -0400, Bulgrien, Dennis wrote:
> Please double-check https://tldp.org/LDP/abs/html/opprecedence.html which states that && AND has higher precedence that || OR. Other web sites and my experience indicate your documentation is incorrect. In the example below, if && was executed first then echo would have to run to complete the expression prior to running false, but in actuality echo does not run because || runs first and short-circuit condition says there is no point in running echo.
> 
> $ false || false && echo y

Those are "list operators", not logical operators.
From "man bash":

"Of these list operators, && and || have equal precedence"

Your example never executes the echo no matter what the precedence of
the operators is:

$  { false || false ; } && echo y
(no output)

$ false || { false && echo y ; }
(no output)

-- 
| Ian! D. Allen, BA, MMath  -  ####@####.####  - Ottawa, Ontario, Canada
| Home: www.idallen.com  Contact Improvisation Dance: www.contactimprov.ca
| Former college professor (Free/Libre GNU+Linux) at:  teaching.idallen.com
| Improve democracy: www.fairvote.ca and Defend digital freedom: www.eff.org
[<<] [<] Page 1 of 1 [>] [>>]


  ©The Linux Documentation Project, 2014. Listserver maintained by dr Serge Victor on ibiblio.org servers. See current spam statz.