Looking at the BASH manpage on the set -e
:
Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status. [...]
So, if any statement returns a non-zero exit code, the shell will exit.
Taking a look at the BASH manpage, on the let
command:
If the last arg evaluates to 0, let returns 1; 0 is returned otherwise.
But wait! The answer to i++
is a one and not a zero! It should have worked!
Again, the answer is with the BASH manpage on the increment operator:
id++ id--: variable post-increment and post-decrement
Okay, not so clear. Try this shell script:
#!/bin/bash
set -e -v
i=1; let ++i; echo "I am still here"
i=0; let ++i; echo "I am still here"
i=0; ((++i)); echo "I am still here"
Hmmm... that works as expected, and all I did was change i++
to ++i
in each line.
The i++
is a post-increment operator. That means, it increments i
after the let
statement returns a value. Since i
was zero before being incremented, the let
statement returns a non-zero value.
However, the ++i
is a pre-increment operator. That means it increments i
before returning the exit status. Since i
is incremented to a 1
, the exit status becomes a zero.
I hope this makes sense.