1

I have a script that is using the ++ (postfix increment) operator. However the exit status is being set in a way that surprises me. To reproduce:

▶ i=0
▶ ((i++))
▶ echo "$?"   ## Huh?
1
▶ echo "$i"
1
▶ ((i++))
▶ echo "$?"
0
▶ echo "$i"
2

I did not expect ((i++)) to set exit status 1 when incrementing from 0 to 1 but not otherwise. Meanwhile, this works just as I did expect:

▶ i=0
▶ ((i+=1))
▶ echo "$?"
0

As does this:

▶ i=0
▶ ((++i))
▶ echo "$?"
0

Is this a bug? If not, why is the postfix increment operator setting exit status 1 when incrementing 0 to 1 but not otherwise?

(Seen in Bash version 5.0.18(1)-release for Mac OS X and version 4.4.20(1)-release for Linux.)

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97

0 Answers0