21
n=0;
((n++));echo "ret=$?;n=$n;"
((n++));echo "ret=$?;n=$n;"
((n++));echo "ret=$?;n=$n;"

from n=1 on, ((n++)) works correctly,
only when n=0, ((n++)) return error,
and I am using a trap '' ERR that is causing trouble with that

is it some bug?

1
  • I don't think this is a duplicate. Even if you've read the question it's is supposed to be a duplicate of, you still need the information that ((expr)) is equivalent to let "expr" to make the connection. This information is given in the answers to this question.
    – smheidrich
    Jul 26, 2014 at 23:15

3 Answers 3

19

It's because the return value of (( expression )) is not used for error indication. From the bash manpage:

((expression))

The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".

So, in your case, because the value of the expression is zero, the return status of (( ... )) is 1.

1
  • ((0)) and ((2-2)) all failed, thx! Jul 27, 2014 at 0:26
14

The reason is as pmos writes above.

One solution would be to use ((++n)) to do increment. Your expression will never evaluate to zero, and so never look like it causes an error.

1
  • 2
    to avoid the trap, I did this ((n++))&&:, but your answer works best, unless n=-1, as "most times" n=0 it will work fine :) Jul 27, 2014 at 18:36
4

You should do:

echo "$((n+=1))"

It will not raise any traps - its only return comes from echo.

Or if you desire to use it as a standalone, in cases in which $n remains less than some 20 digits the following two forms always return true:

n=$((n+1))

Or :

: "$((n+=1))"
12
  • this also works every time ((n++))&&: Jul 27, 2014 at 18:39
  • @AquariusPower - eww. Why in the world would you call out to a nonportable function to increment a variable when you can portably increment same by just evaluating it in the first place?
    – mikeserv
    Jul 27, 2014 at 19:19
  • my project is in bash :) Jul 27, 2014 at 20:10
  • 1
    @AquariusPower - I mean that someone might someday want to adapt your work, and you shouldnt make it more difficult for no reason. Moreover, you would benefit overall from a more portability aware mindset - understanding how and why things interact on different platforms is a long step in the right direction toward understanding how and why they work at all. And last - have you tried benchmarking your project in a more streamlined shell such as dash vs. bash? My own tests usually point to 2:1 performance benefits when the script is well written.
    – mikeserv
    Jul 28, 2014 at 1:11
  • 1
    mmm.. I have a script that the more performance it have, the best; I saw ((n++)) wont work in dash, I am thinking on recoding it and I think I will try it with dash, thx on the tip :); also I could find no free and good bash to c compillers.. Jul 28, 2014 at 1:31

Not the answer you're looking for? Browse other questions tagged .