At Thursday 29 July 2010, Andrew Benton wrote:
> 
> andy:~$ count=0
> andy:~$ ((count++))
> andy:~$ echo $?
> 1
> andy:~$ ((count++))
> andy:~$ echo $?
> 0
I don't think it's a bug, it's just an effect of:
  1. `((EXPR))' returning a non-zero exit status iff EXPR evaluates
     to zero, and
  2. `var++' being a *post-increment* (i.e. the increment of `var'
     takes place after its previous value has been substituted in
     the expression).

You can verify this with:
  $ i=0
  $ echo $((i++))
  0
  $ echo $i
  1
  $ echo $((++i))
  2
  $ echo $i
  2

> 
> Fix:
> 
> This isn't a fix but I can work around this bug if I use
> ((++count))
Yes, because here `count' is incremented before its value is 
substituted into the expression.

> 
> andy:~$ count=0
> andy:~$ ((++count))
> andy:~$ echo $?
> 0
> andy:~$

HTH,
   Stefano

Reply via email to