Skip to content

fix(test): exit 2 when a binary operator has no right operand - #347

Open
ferdingler wants to merge 9 commits into
strands-agents:mainfrom
ferdingler:fix/test-binop-arity-321
Open

ferdingler wants to merge 9 commits into
strands-agents:mainfrom
ferdingler:fix/test-binop-arity-321

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When [ or test was given a binary operator with nothing after it, like [ x = ], it quietly returned false. The realistic way to hit this is an unquoted empty variable, [ "$HOME" = $e ]. Bash treats that as a usage error: it prints an error and exits 2. So a script checking for 2 or for stderr saw something different from bash. The panic in the original report was already fixed on main. This is the exit status and error that were still missing.

Now [ and test follow bash's argument-count rules here. Two arguments that aren't a unary operator and its operand are an error, and so is the same pair after a leading !. With three arguments, an operator in the middle wins, so [ "$c" = x ] still works when c is ! or (. Words left over in a longer expression, such as a trailing operator after -a, give bash's "too many arguments" error. Both errors exit 2. Unary operators bash has that this shell doesn't support yet (-v, -R, -N, -o) still quietly return false, as before. Another PR for #334 touches the same file for integer operands, so a small merge conflict is possible.

Related Issues

Fixes #321

Documentation PR

N/A

Type of Change

Bug fix

Testing

I added differential cases for the issue's forms and the longer expression, plus an integration test for the exit status and error text. They failed on main and pass with the fix. After review I added a case of valid expressions that must stay quiet, which caught two regressions in my first version. The full test suite passes.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@github-actions github-actions Bot added the bug Something isn't working label Nov 21, 2026

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve — no blockers, three should-fix. The forms from #321 and the other claimed ones now match bash 5.2.15 (exit 2 plus an error), and the suite is green at 651b4f2. The new "words left over" check also fires on some valid expressions that the existing parser doesn't fully consume, so they go from a quiet 0/1 to exit 2 with an error.

  • 🟡 x=; [ ! $x = b ] and c='!'; [ "$c" = x ] (also () now print [: too many arguments and exit 2; bash and main exit 1. A six-line three-argument rule fixes it (inline, tested).
  • 🟡 [ ! -v FOO ] and [ -o errexit -a -n x ] now exit 2, though the description says -v/-R/-N/-o behave as before (inline, a question).
  • 🟡 Four of the new guards survive deletion with all tests green (inline).
What I verified, and non-blocking notes

Head 651b4f2, base dbb6189, bash 5.2.15, debug builds on Linux aarch64 only.

Suites (head)

  • cargo test --workspace --all-targets: exit 0, 2734 passed, 0 failed, 0 ignored. The new integration test ran; cargo test --test differential -- --nocapture reports 109 passed and 17 known differences, 126 cases in total including both new ones.
  • cargo fmt --check clean; cargo clippy --workspace --all-targets has no warnings in the changed code.
  • "Failed on main" holds: with only the test changes applied to dbb6189, both new differential cases and the new integration test fail (rc=1 where 2 is expected).
  • Both .expected files match bash byte for byte in the README's recording environment.

Three-way differential (bash / base / head), exit status plus whether stderr is empty, each vector run as [ … ] and as test …:

Run Cases Fixed Regressed Pre-existing Changed, still wrong
≤4 args, 27 tokens, exhaustive 1,103,762 999,958 8,701 47,310 7,809
5 args, 15 tokens, exhaustive 1,518,750 1,377,985 11,476 69,531 4,992
6–8 args, 23 tokens, random 300,000 295,489 561 1,975 373
≤4 args, 18 odd tokens 222,302 196,610 1,472 13,752 1,036
Total, de-duplicated 3,122,592 2,852,942 21,846 130,348 13,974

"Regressed" means bash and base agree and head differs. Every one is head exiting 2 with a message where bash and base exit 0 or 1; nothing flipped between 0 and 1, and there was no panic or hang. 19,222 of the 21,846 are the -v/-R/-N/-o shapes, which says more about the token alphabet than about real scripts.

Also held: [[ … ]] is unchanged (src/exec.rs is identical and only uses the untouched is_*_op/eval_*). The new exit 2 behaves like bash inside if, while, &&, ||, !, set -e, subshells and with stderr redirected (63 probes).

Appendix — non-blocking (5)

  • c=a; test "$c" = "]" exits 1 in bash and on base, 2 here, and the message names [. a=x; b=']'; test "$a" -o "$b" goes from 0 to 2. The cause is the older trailing-] strip at src/builtins/test.rs:13-19 applying to test too; a real fix needs the invoked name, which builtins aren't given (src/builtins/mod.rs:68). Unlikely input, not filed.
  • x=; [ ! ! "$x" ] and a=-n; b=x; [ ! "$a" -a "$b" ] go from 1 to 2 (bash 1). Contrived.
  • Same family as #321 and still silent, on base too: [ a = a -a ] (bash 2, head 1) and e=; [ -n x -o $e ] (bash 2, head 0). Possible follow-up, not filed.
  • tests/differential/cases/test_binary_operator_without_right_operand.sh:2 uses $HOME, which the README says to avoid. It can't change the result here (rc=2 with any value, or unset); a literal would follow the README.
  • Unrelated and on base too: cmd &> file is read as cmd & then > file (ls /nonexistent &> /tmp/x; echo "rc=$?" prints rc=0 and nothing lands in the file; bash prints rc=2). Not filed; I can open an issue if you want one.

Comment thread src/builtins/test.rs Resolved
Comment thread src/builtins/test.rs Resolved
Comment thread tests/shell_integration.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: changes requested — one 🔴, and it comes from the shortcut I suggested. The three earlier findings are fixed at 2dcdec9. I replied on each thread but could not resolve them (my account lacks permission for that), so they are yours to close.

  • ✅ ! / ( comparisons match bash again, and c='!'; [ "$c" = '!' ] is now true like bash.
  • ✅ -v/-R/-N/-o are a quiet false as on main, in longer expressions too.
  • ✅ Tests: six mutations of the guards each fail a differential case now.
  • 🔴 x=; [ ! $x -lt 3 ] is now true (bash exits 2, main exits 1), and #348 as written would not fix it. One-line fix inline, tested alone and merged with #348.

Correction to my first review: the &> gap I offered to file is already recorded as the redirect_both_ampersand known difference, so ignore that offer.

What I verified at 2dcdec9

Head 2dcdec9, previous head 651b4f2, base dbb6189, bash 5.2.15, debug builds on Linux aarch64 only.

Suites (head)

  • cargo test --workspace --all-targets --no-fail-fast: exit 0, 2734 passed, 0 failed, 0 ignored. cargo test --test differential -- --nocapture: 110 passed, 17 known differences, 127 cases including the new test_valid_expressions_stay_quiet.
  • cargo fmt --check clean; cargo clippy --workspace --all-targets exits 0 with no warnings in src/builtins/test.rs.
  • Mutations of src/builtins/test.rs, one at a time, run with cargo test --no-fail-fast --lib --test differential --test shell_integration: all six exit 101.

Three-way differential (bash / base / head), same script and alphabets as last round, each vector as [ … ] and test …. "Regressed" is bash and base agreeing while head differs. Counts are per run, not de-duplicated.

Run Cases Regressed at 651b4f2 Regressed at 2dcdec9
≤4 args, 27 tokens, exhaustive 1,103,762 8,701 827
5 args, 15 tokens, exhaustive 1,518,750 11,476 946
6–8 args, 23 tokens, random 300,000 561 30
≤4 args, 18 odd tokens 222,302 1,472 312
  • Fewer vectors count as fixed than last round (for example 824,196 instead of 999,958 in the first run). That is the exemption working as described: malformed expressions that contain -v/-R/-N/-o are quiet again, as on main.
  • In the [ … ] spelling, the vectors where head exits 0 while bash and base both don't number 50 across the four runs, and all 50 are the three-word integer shortcut in the inline comment.
  • The rest of the [ … ] regressions exit 2 with a message where bash and base exit 1: a doubled !, ! before a three-word -a/-o test, (-led shapes such as c='('; [ ! "$c" = '(' ], and -t shapes. I rated these contrived last round and still do.
  • The test … ] rows from last round's appendix are unchanged.

Comment thread src/builtins/test.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve. The 🔴 from last round is fixed at 80e9467, and I found nothing new.

  • ✅ x=; if [ ! $x -lt 3 ] takes the else branch again. All six integer operators with ! or ( on the left exit 2 with an error, like bash; [ 1 -lt 3 ] and the ! / ( string comparisons are unchanged.
  • ✅ The new line in test_binary_operator_without_right_operand.sh pins it: without the exclusion that case fails.
  • ✅ It composes with #348: a trial merge with its current head 581183f, both sides of the conflict kept, passes, and e=; [ "$e" -lt 3 ] exits 2 there.

All four of my threads are answered and still open, since I can't resolve them.

What I verified at 80e9467

Head 80e9467, previous head 2dcdec9, base dbb6189, bash 5.2.15, debug builds on Linux aarch64 only.

  • cargo test --workspace --all-targets --no-fail-fast: exit 0, 2734 passed, 0 failed, 0 ignored. cargo test --test differential -- --nocapture: 110 passed, 17 known differences, 127 cases.
  • cargo fmt --check clean; cargo clippy --workspace --all-targets exits 0 with no warnings in src/builtins/test.rs.
  • The three test_* differential cases this PR adds match bash's stdout and stderr presence in the README's recording environment.
  • Mutation: with the integer-operator exclusion removed, cargo test --no-fail-fast --lib --test differential --test shell_integration exits 101 on test_binary_operator_without_right_operand.
  • Trial merge of 80e9467 with #348 at 581183f, conflicts in src/builtins/test.rs resolved by keeping both sides: lib 86, differential 1 and shell_integration 2066 passed.

Three-way differential (bash / base / head), same four runs as before (3,144,814 cases, not de-duplicated):

  • No vector regresses at 80e9467 that didn't at 2dcdec9. Per-run regression counts are 825, 946, 30 and 312 (827, 946, 30, 312 last round).
  • In the [ … ] spelling no vector exits 0 while bash and base both don't; last round there were 50.
  • In the test spelling 94 vectors still do. All are four words ending in a literal ], such as test ! != x ], where the older trailing-] strip turns them into a three-word comparison. That is the item from my first review's appendix; I don't think a real script writes it.
  • The other remaining regressions are the contrived shapes listed last round (a doubled !, ! before a three-word -a/-o test, (-led and -t shapes). They exit 2 with a message where bash and base exit 1.

akujane
akujane previously approved these changes Nov 26, 2026

@kevin-orellana kevin-orellana left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing a string with a literal ] now reports a missing operand. The test command accepts ] as data. Only the [ form needs a closing bracket. Could we use the invoked command name to decide when to remove the final ]?

test x = ']' returns 1 with no diagnostic in Bash and on main, as expected for two unequal strings. This branch exits 2 with unary operator expected.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@kevin-orellana Good catch, thanks. test and [ are now separate entry points. Only [ strips the final ], and it reports missing `]' (exit 2) when that word is absent, like bash. Under test, ] is an ordinary word, so test x = ']' returns 1 with no diagnostic again.

  • 25ad104 adds tests/differential/cases/test_closing_bracket_as_operand.sh, recorded from bash 5.2.15. It covers test x = ']', test ']', test x ']', test ! ']', [ x = ] ], [ x = ] and [ x. It failed on the previous head.
  • 977d28b is the fix, and the case passes now.

I also merged main into the branch (183c20f).

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve. The test / [ split in 977d28b fixes the trailing-] problem kevin-orellana found, and I found nothing new.

  • ✅ Under test, ] is an ordinary word again: test x = ']' exits 1 quietly, and test ']' = ']' and test ']' are now true like bash (both false on main).
  • ✅ [ without its closing ] reports missing `]' and exits 2 like bash, including through command [, builtin [, and a function wrapping [ "$@". /bin/test matches too.
  • ✅ The new test_closing_bracket_as_operand case pins it: putting the old strip back under test fails it.
  • ⚠️ The red Rust (windows-latest) job is not from this PR. compound_pipe_into_function_redirected_from_cli overflows the stack, and #348 fails the same way after merging the same main. Filed as #406.
What I verified at 977d28b

Head 977d28b (with main 4f838f9 merged in), compared against main 4f838f9, bash 5.2.15, debug builds on Linux aarch64 only. I couldn't run the Windows job.

  • cargo test --workspace --all-targets --no-fail-fast: exit 0, 2775 passed, 0 failed, 0 ignored. Differential: 132 passed, 17 known differences, 149 cases.
  • cargo fmt --check clean; clippy reports nothing in src/builtins/test.rs or src/builtins/mod.rs.
  • builtin_test and builtin_bracket are dispatched only from src/builtins/mod.rs:68-69; /bin/test goes through the same name.
  • 37 hand probes covering test/[, indirect calls (command, builtin, eval, functions, /bin/test, xargs) and the earlier findings: every one matches bash except xargs test x = < /dev/null. GNU xargs runs the command once on empty input; this shell doesn't. That gap is the same on main and isn't related to this PR.
  • Exhaustive differential, up to 4 args from 27 tokens (1,103,762 cases), against the new main:
    • The 94 test … ] vectors that were wrongly true at 80e9467 (e.g. test ! != x ]) are fixed.
    • 137 vectors still exit 0 where bash exits 2. All are integer tests on non-integers, e.g. test x -eq ]: ] is now an operand, so they hit the #334 bug that #348 fixes.
    • 77 vectors are newly quiet-false where bash is true, e.g. test x -o -v ]. They come from the deliberate -v/-R/-N/-o exemption now that ] counts as a word. I think these are contrived.

This branch has not been deployed

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [ / test panics when a binary operator is the last argument ([ x = ])

4 participants