bug-bash
[Top][All Lists]
Advanced

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

Logical operators in arithmetic evaluation: documentation vs implementat


From: address@hidden
Subject: Logical operators in arithmetic evaluation: documentation vs implementation
Date: Thu, 18 Apr 2013 11:59:40 +0200

The ARITHMETIC EVALUATION section of the man page claims equivalence with C for 
all the operators, but in reality bash does not perform short circuit 
evaluation, which implies that the logical operators do NOT produce the same 
results as in C.
Try these, for example:

f () {
# echo "$@" >&2
    local n=$1
    echo $((0 < n ? n * $(f $((n-1))) : 1))
}

or

g() {
# echo "$@" >&2
    local a=$1 b=$2
    echo $((0 == b ? a : $(g b $((a%b)))))
}

Note that && and || are affected the same way, and the side effect is not due 
solely to recursion. 

$ echo $((1 || $(echo + >&2 && echo 0)))

$ echo $((0 && $(echo + >&2 && echo 1)))

The results are correct, but the side effects are NOT the same as in C.

This may all be fine and as intended, but in that case the documentation feels 
somewhat misleading.

Best,

--@;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]