Repository navigation
Description
Summary
When a shell function is invoked with a call-site redirect and the body runs an external command, the redirected stdin is lost at the first child spawn: the next read returns EOF immediately, so the function processes only the first line. GNU bash 5.3 keeps reading the redirected file. Reproduced in both sh and bash core modes.
Minimal reproduction
printf 'l1\nl2\nl3\nl4\n' > f
F () { while read l; do echo "$l"; /usr/bin/true; done; }
F < f- rubash / Niubash 1.4.0: prints
l1only - GNU bash 5.3: prints
l1,l2,l3,l4
Direct look at the stream after the spawn:
F () {
while read l; do
echo "first[$l]"
/usr/bin/true
if read l2; then echo "second[$l2]"; else echo "second=EOF"; fi
return
done
}
F < f- rubash:
first[l1]thensecond=EOF - GNU bash 5.3:
first[l1]thensecond[l2]
What triggers it and what does not
Loop reads a 4-line file, /usr/bin/true (external) in the body unless noted:
| # | Shape | rubash | GNU bash 5.3 |
|---|---|---|---|
| 1 | function + F < f call-site redirect |
count=1 ❌ | count=4 ✅ |
| 2 | subshell ( ... ) < f |
4 ✅ | 4 ✅ |
| 3 | group { ...; } < f |
4 ✅ | 4 ✅ |
| 4 | top-level while ...; done < f |
4 ✅ | 4 ✅ |
| 5 | redirect written inside the function body | 4 ✅ | 4 ✅ |
| 6 | function + call-site redirect, next read observed |
EOF ❌ |
next line ✅ |
| 7 | function + call-site redirect, builtin true in the body |
4 ✅ | 4 ✅ |
So the trigger is exactly function + call-site redirect + external command in the body. Builtins do not trigger it, and body-internal redirects, group redirects, top-level redirects and subshells are all fine.
Additional observations:
-
The first iteration reads correctly: the redirect is live at function entry and dies at the child spawn.
-
The outer stdin is unaffected — after the function returns, the caller's own stdin is intact:
printf 'O1\nO2\nO3\n' | sh -c ' F () { while read l; do echo "F[$l]"; /usr/bin/true; done; } F < f while read l; do echo "OUT[$l]"; done' # prints F[l1], then OUT[O1], OUT[O2], OUT[O3]
-
Both invocation modes are affected:
sh -candbash -cbehave identically (checked with the Niubash 1.4.0sh.exe/bash.exelaunchers pointing atniu.exe, and with a directly built engine binary).
Impact
- This is what makes the upstream git test suite unusable on such a host:
test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info"is a function called with a call-site redirect whose body runs external commands, so only the first line is read and every test then fails withundefined key 'zero'(ok=0 notok=0). - Real-world scripts are equally exposed: any
func < filewhose body calls external commands (e.g. git hooks) silently processes only the first input line.
Version / bisect notes
- Affected: Niubash 1.4.0 (installed
sh.exe/bash.exe) and a cargo build of rubash at754ba71b— the parent of52b303eb("fix([bug] exec cmd inside command substitution loses inherited stdin - $(exec cat) sees EOF (modernish stdin-script mode runs only its first line; rubash#282 receipt follow-up) #575): exec'd child keeps the shell's stdin channel in every context (fix(#575): exec'd child keeps the shell's stdin channel in every context #583)"). - Since
52b303eb^fails identically, this is not introduced by fix(#575): exec'd child keeps the shell's stdin channel in every context #583; it predates it. - GNU bash 5.3 (Git for Windows build) is correct in every shape above.
- Environment: Windows x64, rustc/cargo from a current nightly toolchain; the same behaviour shows up through Niubash 1.4.0 so it is present in the shipped product as well.
Activity
caomengxuan666 commentedon Oct 16, 2026
Additional datapoint: still reproduces on current master.
6e9b2b04(master HEAD,cargo build --release), both modes:F () { while read l; do echo "iter[$l]"; /usr/bin/true; done; }; F < f-> printsiter[l1]onlyfirst[l1]thensecond=EOF{ while ...; done; } < f-> 4/4 correct754ba71b(the parent of52b303eb) and in Niubash 1.4.0 as shipped (sh.exe/bash.exe->niu.exe), so this is a long-standing defect, not a recent regression.