bug-bash
[Top][All Lists]
Advanced

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

Re: Logical operators in arithmetic evaluation: documentation vs impleme


From: Dan Douglas
Subject: Re: Logical operators in arithmetic evaluation: documentation vs implementation
Date: Thu, 18 Apr 2013 12:02:55 -0500
User-agent: KMail/4.8.3 (Linux/3.4.6-pf+; KDE/4.8.3; x86_64; ; )

Arithmetic operators short-circuit just as in C. The rules for arithmetic 
apply only to the actual arithmetic evaluation step. Arithmetic contexts 
evaluate expressions derived from the results of prior expansions. Think of 
shell arithmetic as a mini-language.

`a' becomes 10 here, because the side-effectful expansion happens before 
arithmetic is evaluated.

     $ ( set -x; a= b=; (( a=b, a+=${b:=5} )); echo "$a" )
    + a=
    + b=
    + ((  a=b, a+=5  ))
    + echo 10
    10

There are 3 completely separate sets of short-circuiting && and || operators 
in different contexts. This will yield 0 for initial values of `a' between 1 
and 4:

    a=2; [[ 0 -ne --a" && "--a && --a -ne 0 ]] && let --a; echo $a

10 factorial (bash/ksh93/zsh):

     $ f=n=n?n--*f:1 let n=10 f
     $ echo "$n"
    3628800

-- 
Dan Douglas



reply via email to

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