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)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0