Why can `((count++))` kill a script under `set -e`?
IntermediateConceptFunctions, exit codes, strict mode and traps
Answer
(( expr )) returns 1 when the expression evaluates to 0. Post-increment returns the old value, so when count is 0, ((count++)) evaluates to 0, returns status 1, and set -e exits.
set -e
count=0
((count++)) # script exits here
((++count)) # fine (evaluates to 1)
count=$((count + 1)) # clearest and always safe
More on Functions, exit codes, strict mode and traps
How do you define a function, pass arguments and return a value?BeginnerWhat does `local` do, and what is dynamic scoping?AdvancedWhy is `local out=$(cmd)` a problem under `set -e`?AdvancedWhat are the conventional exit codes, and what do 126, 127, 130, 137 and 143 mean?BeginnerWhat does `set -e` do, and where does it NOT cause an exit?AdvancedExplain `set -euo pipefail` line by line, and add the `-E` flag.Beginner