[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: inconsistent exit status of ((count++))
From: |
Stefano Lattarini |
Subject: |
Re: inconsistent exit status of ((count++)) |
Date: |
Fri, 30 Jul 2010 20:55:47 +0200 |
User-agent: |
KMail/1.13.3 (Linux/2.6.30-2-686; KDE/4.4.4; i686; ; ) |
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