Skip to content

[BUG] read at end of input returns the wrong status and keeps the old value, so while read -r x || [ -n "$x" ] never ends #362

Open

Description

@strandly-the-agent

Checks

  • I have updated to the latest minor and patch version of Strands Shell
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Binding

Rust

Strands Shell Version

main @ dbb6189 (built from source)

Language Runtime Version

Rust 1.100.0

Operating System

Debian 12 (aarch64)

Installation Method

built from source

Steps to Reproduce

# 1. last line has no trailing newline
strands-shell -c 'printf "a" | { read x; echo "rc=$? [$x]"; }'
# 2. nothing left to read
strands-shell -c 'x=old; read x < /dev/null; echo "rc=$? [$x]"'
# 3. the common idiom for files without a final newline (bounded here so it stops)
strands-shell -c 'printf "a\nb" > f; n=0; while read -r x || [ -n "$x" ]; do echo "[$x]"; n=$((n+1)); [ $n -gt 4 ] && break; done < f'

Expected Behavior

As in bash 5.2.15:

  1. rc=1 [a]. The value is assigned, and the status is 1 because end of input came before a newline.
  2. rc=1 []. At end of input the variable is set to empty.
  3. [a] then [b], and the loop ends.

Actual Behavior

  1. rc=0 [a]
  2. rc=1 [old]
  3. [a] [b] [b] [b] [b]. Without the break the loop never ends and runs into the timeout, because read fails but $x still holds b.

Also, printf "a\nb" | while read x; do echo "[$x]"; done prints [a] [b] where bash prints only [a].

Additional Context

src/builtins/read.rs:122-124 returns 1 at end of input without assigning the variables. src/builtins/read.rs:184-189 returns 1 for a missing delimiter only when -d was given, so the default newline delimiter returns 0.

Found while reviewing #345. It is the same on that branch and on main, so it isn't caused by that change.

Possible Solution

Return 1 whenever end of input came before the delimiter, with or without -d, and assign empty values to the variables when nothing was read. A differential case with the three scripts above would cover it.

Related Issues

#345, #343

Activity

  1. added
    bug
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions