tech-userlevel archive

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

Re: Results of || and && operators in /bin/sh $(( )) evaluation



  Hello,

Robert Elz <kre%munnari.OZ.AU@localhost> writes:

> Currently, NetBSD's /bin/sh (all versions since $(( )) was added, back
> in 1994 it seems) calculate the result of
>
> 	x && y
>
> as 0 if x == 0, otherwise y, and calculate
>
> 	x || y
>
> as x if x != 0, and as y otherwise.
>
> That is, 3 && 4 evaluates to 4, and 3 || 4 evaluates to 3.
>
> In C, these operators always produce 1 or 0 (true or false), regardless
> of the values of the operands.
>
> So does every other shell I can find to test.
>
> I have just submitted PR bin/50960 about this issue (and it would probably
> be better if you replied to that PR, rather than this e-mail).
>
> I have a fix, it is trivial, but I don't want to implement if someone
> in the NetBSD community (or more importantly, something in the system) is
> relying upon this odd behaviour.

It is not odd, it follows well-established long-standing tradition that
is reflected in, e.g.:

"AND evaluates each [expression] until a value of NIL is found or the
end of the list is encountered. If a non-NIL value is the last value it
is returned, or NIL is returned."

"...expressions which are evaluated in order of their appearance. When
one is found to be non-NIL it is returned as the value of OR. If all are
NIL, NIL is returned."
(The Standard Lisp Report, ca. 1982)

"If all forms but the last evaluate to true values, <<and>> returns the
results produced by evaluating the last form."

"If the evaluation of any form other than the last returns a primary
value that is true, <<or>> immediately returns that value..."
(Common Lisp HyperSpec, 1996)

"If every form but the last evaluates to a non-nil value, <<and>> returns
whatever the last form returns."

"If any form other than the last evaluates to something other than nil,
<<or>> immediately returns that non-nil value without evaluating the
remaining forms."
(Common Lisp the Language, 2nd ed., 1994)

"The <test> expressions are evaluated from left to right, and the value
of the first expression that evaluates to a false value (see [Booleans])
is returned. Any remaining expressions are not evaluated. If all the
expressions evaluate to true values, the value of the last expression is
returned."

"The <test> expressions are evaluated from left to right, and the value
of the first expression that evaluates to a true value (see [Booleans])
is returned."
(Revised^4 Report on Scheme, 1991)

I don't have earlier references at hand.


-- 
HE CE3OH...



Home | Main Index | Thread Index | Old Index