Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by cuonglm, glenn jackman, jasonwryan, vonbrand, Braiam Jul 26 at 22:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Works for me just fine using bash 4.3: n=0;((n++));((n++)); echo $n; echo $? which version of bash are you using? –  josten Jul 26 at 22:51
    
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. –  pmos Jul 26 at 23:15

3 Answers 3

up vote 6 down vote accepted

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.

share|improve this answer
    
((0)) and ((2-2)) all failed, thx! –  Aquarius Power Jul 27 at 0:26

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.

share|improve this answer
    
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 :) –  Aquarius Power Jul 27 at 18:36

You should do:

echo $((n=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=n+1))
share|improve this answer
    
this also works every time ((n++))&&: –  Aquarius Power Jul 27 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 at 19:19
    
my project is in bash :) –  Aquarius Power Jul 27 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 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.. –  Aquarius Power Jul 28 at 1:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.