Subject: bin/23814: documenting short-circuit operators as left-associative
To: None <gnats-bugs@gnats.netbsd.org>
From: None <vax@carolina.rr.com>
List: netbsd-bugs
Date: 12/21/2003 00:27:24
>Number:         23814
>Category:       bin
>Synopsis:       documenting short-circuit operators as left-associative
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          doc-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Dec 21 05:42:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     VaX#n8
>Release:        NetBSD 1.5.1
>Organization:
	
>Environment:
	


>Description:
	
The short-circuit operators (&& and ||) in ash (and bash) are left-associative.
This is not necessarily intuitive, because some people may think that the shell
should not parse the rest of a line if the LHS of an OR evaluates to true.
Calling them short-circuit operators, in conjunction with our (and our
program's) left-to-right reading order, might lead you to believe they are
right-associative.  In C and PERL, by contrast, && has higher precedence
than ||.
>How-To-Repeat:
	
$ true || echo foo && echo bar
bar
>Fix:
	

--- sh.1~	Sat Dec 20 23:57:30 2003
+++ sh.1	Sun Dec 21 00:23:04 2003
@@ -575,7 +575,12 @@
 .Dq &&
 and
 .Dq ||
-both have the same priority.
+both have the same priority.  Note that these operators are left-associative,
+so
+.Dq true || echo bar && echo baz
+echoes
+.Dq baz
+and nothing else.  This is not the way it works in C.
 .Ss Flow-Control Constructs -- if, while, for, case
 The syntax of the if command is
 .Bd -literal -offset indent
>Release-Note:
>Audit-Trail:
>Unformatted: