Skip to content

feat(sh): run command strings with sh -c - #212

Merged
ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:feat/sh-dash-c
Nov 12, 2026
Merged

ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:feat/sh-dash-c

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

sh and lash always treated their first argument as a script file, so sh -c didn't work at all:

$ sh -c 'echo hi'
strands-shell: -c: no such file or directory: /home/lash/-c

Now sh -c 'cmd' [name [args...]] runs the string the same way sh script.sh runs a script: in a child shell, so variables and cd don't leak back. $0 is the name you pass, or sh/lash by default, and $1... are the rest. It also skips a leading -- and errors with status 2 when the string is missing, like bash. Piped stdin and $(...) capture work the same way they do for scripts. It touches the same script-running code as #199, but #199 still applies cleanly on top.

I compared the cases against bash on Linux: positional args, exit status, multiple statements, redirects, --, the missing-argument error, and nothing leaking into the parent. The new tests fail on main and pass with the change, and sh script.sh args behaves the same as before.

Related Issues

Part of #207

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • 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
ferdingler marked this pull request as ready for review November 12, 2026 04:53
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 04:53
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent please review

@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 once rebased. The -c handling is correct and small. I found no bugs in the new logic. The PR is CONFLICTING with main in src/exec.rs, and resolving it naively loses #248's script-name parse errors (see the inline comment).

  • 🟡 Rebase: pass an origin through run_sourced → execute_source_file. There's a suggestion inline.
  • 🟡 Add a test for the -c path through the kernel command check: tests/command_check.rs (#204, on main only) covers sh /tmp/s in nested_commands_are_checked but not sh -c. After the rebase, "sh -c 'rm /tmp/x'" and "sh -c '\"$0\" /tmp/x' rm" both pass. Add them so this new nesting path can't silently bypass Kernel::check_command later.
  • I filed #250 for a pre-existing bug this makes easier to hit: set -e; sh -c 'false; echo x' aborts the parent, because the child inherits set -e/-u/-x.

The adversarial pass timed out after 900s on its first run. The re-run completed and found nothing new that this PR introduces.

✅ What I verified
  • Head caa0801: cargo test --test shell_integration passed 1569/1569, including all new sh_c_*/lash_c_* tests and script_args_and_isolation_unchanged. cargo test --test differential passes. cargo fmt --check is clean. cargo clippy --all-targets adds no new warnings (the only one is the existing grep_F_fixed naming). CI is green.
  • I merged into main a064834 with the inline resolution: shell_integration passed 1607 tests, differential passed, command_check passed 23 (with the 2 cases above added).
  • Matches bash: $0/$1/$#/$@, shift, exit N / return N and $?, -- handling (sh -c -- cmd n a, sh -c cmd -- a → $0=--), the missing-argument error with status 2, nested sh -c "sh -c … inner" outer, $(sh -c …), sh -c … | cmd, redirects, cd/export/set -e in the child not leaking to the parent, $0/args restored after returning (from top level, from inside a function, from inside a script), sh script.sh $0 unchanged, and recursion bounded by the max-depth error.
Questions (non-blocking)
  • sh -ec/-xc/-euc still go down the file path (-ec: no such file or directory, 126), and sh -c -e cmd runs -e as the command. Agents write these a lot. Is a follow-up planned under #207?
  • bash -c returns 127 (bash: command not found). Is aliasing bash in scope for #207?
  • The PR body says $(...) capture doesn't work yet, but on this head x=$(sh -c 'echo hi') gives hi. Worth updating the description.
Appendix: pre-existing, not from this PR (same on `sh script.sh`)
  • Child inherits set -e/-u/-x: filed #250.
  • Fork leakage (same root cause as #250, mentioned there): the parent's unexported vars (X=1; sh -c 'echo $X' → 1, bash prints empty), its functions (f(){ echo fn; }; sh -c f), and its aliases are all visible in the child.
  • An EXIT trap doesn't fire in a child: the trap only runs at proc.depth == 0 (src/exec.rs ~L202 on main). trap "echo bye" EXIT in sh /tmp/t.sh doesn't fire either. Not filed; it belongs with the #207 gaps.
  • xtrace and stderr ordering: the child's stdout and stderr are captured separately and flushed stdout-first, so set -x lines and >&2 output come after stdout. Scripts do the same.
  • false; sh -c 'echo $?' prints 1 (bash prints 0). A syntax error in the child exits 1 (bash exits 2). "" arguments are dropped during expansion, so sh -c '' gives status 2 (bash gives 0). sh -- -c opens --. Piped stdin into sh -c is empty (you already noted this; #224 addresses it).

Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Merged main (4a5ba2a) and pushed 8aa1535.

  • Origin in parse errors: fixed, details in the inline thread.
  • command_check coverage: I added "sh -c 'rm /tmp/x'" and "sh -c '\"$0\" /tmp/x' rm" to nested_commands_are_checked. Both pass, so the -c path goes through Kernel::check_command and now there's a test that would catch it if it stopped.
  • [BUG] sh script / sh -c child shells inherit the parent's set -e / -u / -x #250: thanks for filing it, I'll keep that separate.

On the questions:

Full local run on 8aa1535: cargo test --workspace --all-targets (2219 passed, 0 failed), --doc, --lib --features node, cargo fmt --check and cargo clippy all clean, no new warnings.

@ferdingler
ferdingler merged commit dc251b8 into strands-agents:main Nov 12, 2026
30 checks passed
@ferdingler
ferdingler deleted the feat/sh-dash-c branch November 12, 2026 23:45
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