You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[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
# 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:
rc=1 [a]. The value is assigned, and the status is 1 because end of input came before a newline.
rc=1 []. At end of input the variable is set to empty.
[a] then [b], and the loop ends.
Actual Behavior
rc=0 [a]
rc=1 [old]
[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.
Activity