Skip to content

fix: match bash on head -c, exec redirects, and read -d - #307

Merged
ferdingler merged 4 commits into
strands-agents:mainfrom
fengqiaoqu:fix/stdin-head-exec-read-d
Nov 21, 2026
Merged

ferdingler merged 4 commits into
strands-agents:mainfrom
fengqiaoqu:fix/stdin-head-exec-read-d

Conversation

@fengqiaoqu

Copy link
Copy Markdown
Contributor

Description

ferdingler asked me to re-file this after closing strands-agents/box#442: Box no longer vendors Shell since box#414, and these three gaps were still present upstream, with no open PR covering them.

  1. x=$(head -c 1); cat dropped the rest of stdin. head takes fd 0 out of the process and did not put the remaining reader back, so leftover bytes were discarded with it. After a head that read stdin, fd 0 is restored.
  2. exec < /dev/null with no command returned 0 and left stdin unchanged (there was a TODO for this). Those redirects are now applied to the current shell, so later commands see them.
  3. read -d '' did not parse -d. An empty delimiter is NUL, as in bash: the builtin reads until that byte or EOF, assigns what it read, trims trailing IFS, and returns 1 when EOF arrives before the delimiter.

I re-checked these on current main before opening this; the three tests below failed there and pass with the change.

Related Issues

Re-filed from strands-agents/box#440 / strands-agents/box#442, at ferdingler's request.

Documentation PR

None.

Type of Change

Bug fix

Testing

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

The three new tests failed on unmodified main and pass here:

  • head_bytes_in_a_substitution_leaves_the_rest_of_stdin
  • exec_without_a_command_replaces_stdin
  • read_empty_delimiter_takes_all_input_until_eof

cargo test --workspace --all-targets was green (including 1970 shell_integration tests). cargo fmt --check was clean. cargo clippy --workspace --all-targets reported only existing warnings, none in these files.

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.

fengqiaoqu and others added 2 commits November 14, 2026 10:18
head -c took fd 0 out of the process and dropped the leftover
bytes, so x=$(head -c 1); cat discarded the rest of stdin.
exec < file with no command hit a TODO and left stdin unchanged.
read did not parse -d; an empty delimiter is NUL, as in bash.
@ferdingler

Copy link
Copy Markdown
Collaborator

thanks for the follow up!

@ferdingler

Copy link
Copy Markdown
Collaborator

@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.

Changes requested. The head -c and exec <file fixes hold up. But the no-command exec now applies output redirects as well, and that breaks exec 2>&1 and exec >log, both of which are no-ops on main. read -d also trims more than bash does.

  • 🔴 exec 2>&1 / exec 3>&1 fail with bad fd 1 (rc 1, and set -e aborts). exec >f makes all later output disappear and leaves f empty.
  • 🟡 read -d trims non-whitespace IFS characters, trims REPLY, and strips \r. IFS= read -r -d '' x < file drops the trailing newline (root cause filed as #308).
  • 🟡 read -rd '', the usual spelling of this idiom, still doesn't parse.

@ferdingler, for you as requester.

✅ What I verified
  • Head b7a7c87 (base f5c6ac4). cargo test --test shell_integration: 1970 passed, including the 3 new tests.
  • Compared against bash 5.2.15 and coreutils 9.1 in the sandbox. Held: x=$(head -c 1); cat, sequential head -c 1, -c 0, -c larger than the input, -c -N; exec < f + while read, exec <<EOF, exec </dev/null in functions and groups, ( exec >f ); echo b (no leak), exec </nonexistent (continues, as bash does); read -d , with field splitting, NUL records, -d é (first byte, same as bash), -d with no argument → rc 2.
Appendix: non-blocking (5)
  • ⚪ head -n N still loses the rest of stdin when stdin is a file: BufReader reads ahead and that buffer is dropped before the fd is restored. { head -n 1; cat; } < f prints only the first line; bash prints all of it. Pipes match bash, and this isn't a regression (main lost fd 0 entirely), but the comment at head.rs:104-106 is only accurate for -c.
  • ⚪ The redirect-expansion match now appears five times in exec.rs (:1790, :2130, :2436, :3106, :3402). A small helper would remove the duplication. The IFS lookup is also duplicated in read.rs:89/:109.
  • ⚪ Without -r, a backslash-escaped delimiter still ends the read: read -d , x on a\,b, gives a\ (bash gives a,b). This is part of the existing lack of backslash handling in read.
  • ⚪ read -d "" (double quotes) turns into read -d x because of #299. The test uses '', so it passes.
  • ⚪ Pre-existing, not from this PR, filed as #308: builtins ignore prefix assignments, so IFS=, read a b splits on the shell's IFS.

Comment thread src/exec.rs Resolved
Comment thread src/builtins/read.rs Resolved
Comment thread src/builtins/read.rs Resolved
…ead -d

No-command exec applied output redirects to the session process, which
has no fd 1 or 2, so exec 2>&1 failed with bad fd 1 and exec >file
swallowed later output. Only stdin redirects are applied now.

read -d trims only trailing IFS whitespace, and only when a variable
was named; it keeps CR and leaves REPLY untrimmed. Combined short
flags (-rd, -d:) are parsed.
@ferdingler

Copy link
Copy Markdown
Collaborator

@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.

Approve. All three findings from my last review are fixed at 2a82009, and I found nothing new that blocks. @ferdingler

  • 🔴 no-command exec output redirects: fixed. exec 2>&1, exec >&2, exec 3>&1 and exec >f are no-ops again, and set -e; exec 2>&1; echo after prints after.
  • 🟡 -d trimming: fixed. The first three rows of the table match bash. The fourth matches when IFS= is its own statement; the prefix form IFS= read -r -d '' x still waits on #308.
  • 🟡 read -rd '' / -d:: fixed.

One behaviour change to be aware of: an unsupported flag inside a cluster (read -rs, -rn1, -rt 5, -ra) now fails with invalid option and rc 2. On main these silently assigned to the wrong variable, so I read this as an improvement.

What I verified, and non-blocking notes

Verified at 2a82009 (bash 5.2.15 as reference, main @ ad54fd1 as baseline)

  • cargo test --workspace --all-targets: exit 0, no failures; shell_integration 2058 passed, including the 11 tests this PR adds. CI is green. I did not run cargo fmt or clippy locally.
  • 57 one-off scripts run through strands-shell -c and bash -c. Matching bash: read -rd '', -d:, -d ' ', read -r --, NUL-separated while IFS= read -rd '' f loops, multi-byte input before a delimiter, read -d '' x < file; exec < file followed by a while read loop, inside a function, a subshell, $(…) and a pipeline stage, exec 0< f, exec <> f, exec <<EOF, exec < missing (rc 1, later commands still run); head -c N twice in a row, head -c 0, head -c -2, head -c 2 then read. The ones that differ are the three notes below, plus gaps that are the same on main (<<<, the -p prompt on a non-terminal).
  • Read, not run: the merge with #326 (UTF-8 decoding) keeps the timeout path intact, putting the bytes read so far back on fd 0 (src/builtins/read.rs:109-115).

Appendix: non-blocking (3)

  • ⚪ read -s x (an unsupported flag on its own) is still taken as a variable name, while read -rs x is now an error. Erroring for any unknown option, and accepting -s and -e as no-ops, would make the two consistent. Fine as a follow-up.
  • ⚪ read -d '' x trims trailing IFS whitespace but not leading (printf ' a\n' gives a, bash a). Plain read x has never trimmed either side: pre-existing, filed #343, which covers both.
  • ⚪ IFS=… read -d cases (IFS= read -r -d '' x, IFS='=' read -r -d '' k v) still differ from bash until #308 lands.

@ferdingler
ferdingler merged commit dbb6189 into strands-agents:main Nov 21, 2026
32 of 34 checks passed
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.

3 participants