Skip to content

Fix silent script failures from ((var++)) with set -e#332

Open
emanuel-r wants to merge 1 commit intobuildermethods:mainfrom
emanuel-r:fix/arithmetic-exit-codes
Open

Fix silent script failures from ((var++)) with set -e#332
emanuel-r wants to merge 1 commit intobuildermethods:mainfrom
emanuel-r:fix/arithmetic-exit-codes

Conversation

@emanuel-r
Copy link

Bug #1: Silent Script Failure from ((var++)) with set -e
Severity: High — scripts silently exit with no error message
Problem
Multiple scripts use the pattern ((count++)) to increment counters. When the variable is 0, the expression ((0++)) evaluates to 0, which bash treats as exit status 1 (falsy). Combined with set -e (which all three scripts use), this silently kills the script.
Affected Files and Lines
scripts/project-install.sh:

Line 244: ((profile_file_count++)) — standards installation counter
Line 248: ((profiles_used++)) — profiles used counter
Line 401: ((count++)) — commands installation counter
Line 448: ((chain_depth++)) — inheritance chain display counter

scripts/common-functions.sh:

Line 222: ((count++)) — file copy counter

scripts/sync-to-profile.sh:

Line 197: ((i++)) — profile listing counter
Line 276: ((i++)) — file display counter
Line 452: ((backup_count++)) — backup counter
Line 480: ((sync_count++)) — sync counter

Reproduction
bash# This demonstrates the issue:
bash -c 'set -e; count=0; ((count++)); echo "This never prints"'

Compare with a non-zero start value (works fine):

bash -c 'set -e; count=1; ((count++)); echo "This prints"'
Observed Behavior
Running project-install.sh on a fresh v3 install:

Script prints "Configuration:" and then silently exits
No error message displayed
Exit happens at the inheritance chain display loop (line 448)
If that line is fixed, the script proceeds but installs only 1 of 5 commands before silently exiting at line 401

Fix
Replace all ((var++)) patterns with the POSIX-safe form:
bash# Before (fails when var is 0 with set -e)
((count++))

After (always safe)

count=$((count + 1))
Note: for ((i=0; i<n; i++)) loop constructs are NOT affected — only standalone ((var++)) statements.
References
This is a well-documented bash behavior:

https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html
The (( )) compound command returns exit status 1 when the expression evaluates to 0

@bossSteve03
Copy link

Tested this fix locally and it solves the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants