Skip to content

fix(printf): bash option parsing, usage errors, early -v validation - #685

Merged
brianjfox merged 1 commit into
mainfrom
fix/printf-option-parsing
Aug 20, 2026
Merged

fix(printf): bash option parsing, usage errors, early -v validation#685
brianjfox merged 1 commit into
mainfrom
fix/printf-option-parsing

Conversation

@brianjfox

Copy link
Copy Markdown
Owner

Summary

Fixes #682. printf accepted any option-looking word and returned 0; it now matches bash's internal_getopt(\"v:\") and vflag block:

  • missing format operand → bare usage line to stderr, exit 2 (EX_USAGE)
  • unknown option → printf: -x: invalid option + usage, exit 2
  • -v argument attached (-vNAME) or separate; missing → -v: option requires an argument + usage; printf -v -- takes -- as the option-argument (then fails validation) while a lone - is the format string
  • the -v target is validated before any format processing: plain identifier or IDENTIFIER[non-empty-subscript] ending in ]; a[, a[], 1a[0], a b, invalid-var all report not a valid identifier, exit 2, and conversions never run (printf -v invalid-var '%d' GNU emits only the identifier error)

Verification

Byte-identical vs reference bash 5.3.15 on 21 cases (all forms above plus printf -- -v, printf - hi, valid a[0] element targets, and empty-format no-ops). printf.tests scoreboard 36 → 31; builtins/errors/varenv hold at 0; smoke passes.

Closes #682

🤖 Generated with Claude Code

printf accepted anything and returned 0 (issue #682).  Match bash's
internal_getopt("v:") and vflag block:

- a missing format operand prints the bare usage line, EX_USAGE (2)
- an unknown option is `-x: invalid option' plus usage, EX_USAGE
- -v takes its argument attached (-vNAME) or separate; missing is
  `-v: option requires an argument' plus usage; `printf -v --' takes
  `--' as the argument (and fails validation), while a lone `-' is
  the format string
- the -v target is validated BEFORE any format processing: a plain
  identifier or IDENTIFIER[NON-EMPTY-SUBSCRIPT] with the closing
  bracket last; `a[', `a[]', `1a[0]', `a b' all report
  `not a valid identifier' with EX_USAGE, and conversions never run

Verified byte-identical with bash 5.3.15 on 21 cases; printf.tests
scoreboard 36 -> 31, builtins/errors/varenv hold at 0.

Closes #682
@brianjfox
brianjfox merged commit 7922a10 into main Aug 20, 2026
1 check passed
@brianjfox
brianjfox deleted the fix/printf-option-parsing branch August 20, 2026 07:54
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.

printf: no option/usage errors; -v target validated late and wrongly

1 participant