Skip to content

fix(read): strip IFS whitespace around the value when a name is given - #345

Open
ferdingler wants to merge 2 commits into
strands-agents:mainfrom
ferdingler:fix/read-trim-ifs-whitespace
Open

ferdingler wants to merge 2 commits into
strands-agents:mainfrom
ferdingler:fix/read-trim-ifs-whitespace

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When read was given a variable name, it kept the spaces and tabs around the value that bash strips. A single variable got the line untouched, and the last of several variables kept its trailing whitespace. So something like a count read from padded output wouldn't compare equal to the plain number. read -d had the same problem with leading whitespace.

Now read strips leading and trailing whitespace that's part of IFS whenever a name is given, with or without -d. REPLY and an empty IFS still keep the line as is, same as bash.

Related Issues

Fixes #343

Depends on #353. Until that lands, a prefix IFS= never reaches read, so with this change alone while IFS= read -r line would lose indentation. This should merge after #353.

Documentation PR

None

Type of Change

Bug fix

Testing

I added a differential case recorded from bash 5.2.15 covering one and several variables, -d, REPLY, an empty IFS, and an IFS with only a space. It failed on main and passes with the change, and the full test suite passes. I used IFS=...; before read instead of a prefix assignment, because prefix assignments on builtins are a separate bug (#308).

  • 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

@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: hold this until #353 lands. The trim matches bash 5.2.15 on everything probed, but on its own this branch makes while IFS= read -r line strip indentation, because the prefix IFS= doesn't reach the builtin yet (#308). With #353 merged in locally the idiom matches bash again.

Pass Result
Correctness, scope, tests 🔴 1 (inline). The new case and its .expected are faithful to bash
Adversarial Same 🔴, no other regression against main
Test suite cargo test --workspace --all-targets passes on af4d30d (2,733 passed, 0 failed, 0 ignored)

Small thing: the description cites #205 for the prefix-assignment bug. That one is #308 (#205 is the merged parser PR).

What I verified
  • Head af4d30d against base dbb6189 on Debian 12 aarch64. Oracle: bash 5.2.15 run as env -i PATH=/usr/bin:/bin LC_ALL=C bash -c '…'. Every probe was run on bash, a build of main, and a build of this branch.
  • Re-recorded read_trims_ifs_whitespace.sh from bash in an empty directory: status, stderr and stdout equal the committed .expected. This branch matches it, main differs on 7 of the 10 lines.
  • Matches bash on this branch: one and several names, more and fewer fields than names, -r, --, -d '', -d ,, end of input before the delimiter, read REPLY, no name, and IFS unset, empty, space only, tab only, newline only (all set with IFS=…;). Non-breaking spaces are kept, as in bash.
  • An independent adversarial pass ran a matrix of whitespace shapes against reader forms plus randomized three-way probes. It found the inline regression and nothing else that main got right.
  • Combination: af4d30d merged with #353 (114b758) builds, cargo test --test differential passes, and IFS= read -r x, IFS=: read x, IFS= read -r -d '' f and IFS= command read -r x match bash.
  • Not run: pytest tests/python and npm test. Not tested: macOS and Windows (CI is green there).
Appendix: non-blocking (5)
  1. One behavior changed to match bash but has no case: printf ' a \\\n,' | { read -d , x; echo "[$x]"; } gives [a] on bash and this branch, [ a \] on main. It would pin the trim running after the line-continuation step.
  2. With #353 merged in, IFS= eval 'read -r x' and IFS= f (a function that calls read) still trim on a b , where bash and main give [ a b ]. Those forms are outside what #353 covers, so they belong to #308.
  3. CRLF input: printf ' a b \r\n' | { read x; … } gives [a b] here, [ a b ] on main, and [a b \r] on bash. This follows from the existing CR strip at src/builtins/read.rs:128-130, so I'm only noting that the value changes.
  4. printf 'a\\ \n' | { read x; … }: bash keeps the escaped space ([a ]), this branch gives [a\], main gives [a\ ]. It sits on the existing read_raw_backslash known difference.
  5. Pre-existing, filed #362: read returns 0 for a last line without a newline and keeps the old value at end of input, so while read -r x || [ -n "$x" ] never ends.

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

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent re-review please

@strandly-the-agent

Copy link
Copy Markdown
Collaborator

@ferdingler There's nothing new to review yet. The head is still af4d30d, the commit I reviewed on 2026-11-21, and #353 is still open, so the blocker hasn't changed: merged alone, this branch still makes while IFS= read -r line strip indentation. My changes-requested review stays.

The description update is good: it notes the dependency and now cites #308 instead of #205. One small mismatch: the checklist ticks "Any dependent changes have been merged and published", but #353 hasn't merged yet.

Once #353 lands and you rebase with the IFS= read -r x line added to the case, ping me and I'll re-run the three-way comparison (bash, main, this branch) on the new head.

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] read var keeps leading and trailing whitespace that bash strips

2 participants