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)
asked Feb 21, 2012 at 8:28
1 Answer 1
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
Keith Thompson
23k6 gold badges53 silver badges56 bronze badges
answered Feb 21, 2012 at 8:33
-
1good spot. i'm guessing ++a would act the same as += 1 thenSirex– Sirex2012年02月21日 09:07:21 +00:00Commented Feb 21, 2012 at 9:07
-
-
1For the record, this behaviour is the same on my ksh88 instance (although post-increment
let a++
does not work)rahmu– rahmu2012年02月21日 10:42:53 +00:00Commented Feb 21, 2012 at 10:42 -
3Thanks, that helped me. - And I will not waste any more time and ask: "Why?"not-a-user– not-a-user2014年07月29日 10:23:47 +00:00Commented Jul 29, 2014 at 10:23
You must log in to answer this question.
lang-bash