tech-userlevel archive

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

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

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.

Note, this has nothing whatever to do with the sh command line versions
of && and ||, only to those operators when they appear inside $(( )) and
must produce a numeric result.

So, comments (better if you go find the PR and reply to it, or just
send to with "Re: PR bin/50960" in the Subject.


ps: long term, we may want to do what FreeBSD have done, and replace the
current yacc/lex expression evaluator with a hand written C version.
They (according to the commit logs there) took their version from dash.

Home | Main Index | Thread Index | Old Index