Skip to content

fix nounset arithmetic assignment and sed s///p - #770

Merged
ewhauser merged 1 commit into
mainfrom
codex/arith-sed-harness-compat
Mar 28, 2026
Merged

ewhauser merged 1 commit into
mainfrom
codex/arith-sed-harness-compat

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

What changed

This PR fixes two gbash compatibility issues that were blocking the Harness overlay work:

  • arithmetic plain assignment in (( ... )) and for (( ... )) now initializes unset variables under set -u
  • the builtin sed now supports the p flag on s/// substitutions

Why it changed

Harness scripts were carrying local workarounds for both bugs:

  • reverse index loops like for (( i=${#arr[@]}-1; i>=0; i-- )) failed because arithmetic lvalue resolution treated i=... as a read of an unset variable under nounset
  • hooks using sed -n 's/^cwd: //p' failed because gbash rejected the p substitute flag entirely

Fixing these in gbash lets the example remove workaround logic in a follow-up PR instead of baking compatibility shims into .harness.

Root cause

  • shell/expand/arith.go used the same lvalue path for plain assignment and read-modify-write arithmetic operators. Under NoUnset, unset lvalues errored before = assignments could write a value.
  • internal/builtins/sed.go parsed only g and i for substitute flags and had no execution path for print-on-substitute behavior.

Impact

  • set -u; ((i=2)) and set -u; for (( i=...; ...; ... )) now match Bash instead of failing with unbound variable
  • ++, --, and += on unset variables under set -u still fail, preserving Bash behavior
  • sed -n 's/.../.../p' and sed 's/.../.../p' now behave like sed, including duplicate output when automatic printing is enabled

Validation

  • go test ./shell/expand ./internal/runtime
  • GBASH_CONFORMANCE_DIFF="$(./scripts/ensure-diffutils.sh)" go test ./internal/builtins
  • make lint

@ewhauser ewhauser changed the title [codex] fix nounset arithmetic assignment and sed s///p fix nounset arithmetic assignment and sed s///p Mar 28, 2026
@ewhauser
ewhauser marked this pull request as ready for review March 28, 2026 21:17
@ewhauser
ewhauser merged commit 9920b43 into main Mar 28, 2026
16 checks passed
@ewhauser
ewhauser deleted the codex/arith-sed-harness-compat branch March 28, 2026 21:21
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.

1 participant