Skip to content

fix: pass piped and redirected stdin to sh scripts - #224

Merged
ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/script-piped-stdin
Nov 12, 2026
Merged

ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/script-piped-stdin

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When you pipe or redirect into a script run with sh (or through a shebang), the script never sees that input. So cat inside it prints nothing and read fails:

$ printf 'read l; echo got $l\n' > r.sh
$ echo x | sh r.sh
strands-shell: read: bad fd 0
got

bash prints got x. Now the script gets the stdin of its pipeline stage, the same way shell functions already do, so pipes, < file and heredocs all reach it. The same command prints got x with this change. It should land together with or after #199. Without #199, a script's stdout still ignores pipes and $(...) in -c/REPL mode, so the visible fix there depends on it.

I compared piped, redirected, heredoc and /dev/null input with bash. I also checked a script that reads one line of many, one that doesn't read at all, and that the pipe doesn't leak back to the parent shell afterwards. The new tests fail on main and pass here, and the branch merges cleanly with #199 with the script tests passing on the combination. A script inside a loop or brace group that reads the compound's stdin still behaves differently from bash; that's a separate follow-up.

Related Issues

Part of #207

Documentation PR

Type of Change

Bug fix

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 9, 2026 23:51
@ferdingler
ferdingler requested a review from a team as a code owner November 9, 2026 23:51
@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.

Approve. The 3-line move at src/exec.rs:3553 does what the PR says, and I found no regressions. Piped, redirected, heredoc, and shebang stdin now reach the script. Each case I ran matches bash, and none of them worked on main before.

@ferdingler, one question, not blocking: the body says this depends on #199, but #199 is already on main (merged 11-06), and script stdout through pipes and $(...) works on this branch. You could probably drop that note.

✅ What I verified
  • Head 1775f8f. cargo test --workspace --all-targets: 2156 passed, 0 failed. script_*: 24 passed. The 3 new tests fail on main.
  • I ran about 75 probes against this branch, main @ a064834, and real bash (my runs plus an independent reviewer pass). These now match bash: successive reads, read; cat, while read, a function inside the script, the script in the middle of a pipeline, < file, heredoc, </dev/null, & wait, and $(echo x | sh r.sh).
  • No leaks or hangs. echo x | sh r.sh; read z doesn't reach the parent. A script that doesn't read, or reads one line of about 1.4 MB, still exits.
Appendix: non-blocking (3)
  • ⚪ No test checks that stdin survives across commands inside one script. printf 'a\nb\n' | sh r2.sh with read a; read b would cover it. (Probing shows it works.)
  • ⚪ exec < file inside a piped script now reads the pipe instead of the file. Bash reads L1, this branch reads hi, and main errored. Functions already do the same on main, and exec < doesn't persist even at top level, so this isn't caused by this PR.
  • Not caused by this PR, filed as #249: a command in a while read … done < list body (ls, x | cat, sh s.sh) takes the loop's stdin through exec.rs:3483, so the loop runs only once. Both main and this branch print s:a then read: bad fd 0; bash prints s:a s:b s:c. The same root cause makes a nested sh inner.sh; cat lose unread input. That's the compound-stdin follow-up you mentioned.

@ferdingler
ferdingler merged commit 121ea99 into strands-agents:main Nov 12, 2026
31 checks passed
@ferdingler
ferdingler deleted the fix/script-piped-stdin branch November 12, 2026 17:19
@strandly-the-agent strandly-the-agent mentioned this pull request Nov 13, 2026
8 of 10 tasks
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