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.

Try it:

$ a=0
$ let a++
$ echo $?
1 # Did the world just go mad?
$ echo $a
1 # Yes, it did.
$ let a++
$ echo $?
0 # We have normality.
$ echo $a
2

Contrast with this:

$ b=0
$ let b+=1
$ echo $?
0

And this (from Sirex):

$ c=0
$ let ++c
$ echo $?
0

What is going on here?

$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted

From help let:

Exit Status:
If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise..

Since var++ is post-increment, I guess the last argument does evaluate to zero. Subtle...

A perhaps clearer illustration:

$ let x=-1 ; echo x=$x \$?=$?
x=-1 $?=0
$ let x=0 ; echo x=$x \$?=$?
x=0 $?=1
$ let x=1 ; echo x=$x \$?=$?
x=1 $?=0
$ let x=2 ; echo x=$x \$?=$?
x=2 $?=0
share|improve this answer
1  
good spot. i'm guessing ++a would act the same as += 1 then –  Sirex Feb 21 '12 at 9:07
    
Yep, that works. –  l0b0 Feb 21 '12 at 9:10
1  
For the record, this behaviour is the same on my ksh88 instance (although post-increment let a++ does not work) –  rahmu Feb 21 '12 at 10:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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