Date: Fri, 29 Jun 2001 14:58:35 -0700 From: Dave Tweten <tweten@nas.nasa.gov> To: Dmitriy Kyrhlarov <dimma@electromir.ru> Cc: Pete Fritchman <petef@databits.net>, freebsd-doc@FreeBSD.ORG Subject: Re: Fw: Re: Bourne Shell Syntax Wierdness Message-ID: <200106292158.f5TLwau26504@gilmore.nas.nasa.gov> In-Reply-To: Message from Dmitriy Kyrhlarov <dimma@electromir.ru> of "Fri, 29 Jun 2001 10:54:38 %2B0400." <3B3C262E.B44B479D@electromir.ru>
next in thread | previous in thread | raw e-mail | index | archive | help
dimma@electromir.ru said:
>true || command 1 && command 2
>equivalent:
>true && command 2
>and it's a right...
Not by POSIX.2.
cynic@mail.cz said:
>I'd guess that (since they have the same precedence and
>associativity) is because
>true || true && echo Oops!
>is the same as
>(true || true) && echo Oops!
Also not what POSIX.2 says.
mwm@mired.org said:
>The || and && operators are left associative, so that your example -
>which is ambiguous given only the information you provided - is parsed
>as "( true || true ) && echo Oops!". Since "( true || true )" always
>terminates normally, it should print "echo Oops!".
>>Incidently, "true || { true && echo Oops! }" prints nothing -- which
>>I would have expected with or without the "{}".
>That's what would happen if the operators were right associative
>instead of left associative - which would collide with most peoples
>expectations based on common mathematical usage.
But that isn't what POSIX.2 says when it defines the operators. It says,
effectively, that these operators are right associative. Which, incidently,
corresponds with most C programmers' expectations that these operators will
behave like the operators of the same name in the C language. That's also
what is suggested by the man page when it covers the topic under the heading,
"Short-Circuit List Operators."
^^^^^^^^^^^^^
From POSIX.2:
"command1 [ || command2 ] ...
"First, command1 shall be executed. If its exit status is
nonzero, command2 shall be executed, and so on until a command
has a zero exit status or there are no more commands left to execute."
By implication, if command 1 returns a zero exit status, the rest of the list
up to a possible ";", "&", or <newline>, will not be executed. Also, in
POSIX.2, section 3.9.3,
"The operators && and || shall have equal precedence and shall
be evaluated from beginning to end."
Presumably, "beginning to end" is a bit of English language cultural
chauvinism for "left to right."
As I said in my original message, this variance between Bourne shell behavior,
on the one hand, and POSIX.2, on the other, seems to be universal among Bourne
shells I've been able to test. But, how to explain it on the man page?
Particularly so a C programmer who reads the heading, "Short-Circuit List
Operators," will understand that knowledge of the C-language or of POSIX.2
will only confuse him.
So my conclusion is that the man page seems to be accurate as far as it goes,
but it should also say that Bourne Shell "||" and "&&" operators are left
associative and tharefore are not POSIX.2 compliant.
--
M/S 258-5 | 1024-bit PGP fingerprint: | tweten@nas.nasa.gov
NASA Ames Research Center | 41 B0 89 0A 8F 94 6C 59 | (650) 604-4416
Moffett Field, CA 94035-1000 | 7C 80 10 20 25 C7 2F E6 | FAX: (650) 604-4377
We each earn what freedom of speech we defend for those who most offend us.
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-doc" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200106292158.f5TLwau26504>
