Skip to content

read builtin: empty from /dev/fd/N, and rc=0 at EOF under compound/fd redirection (while read off-by-one) #701

Closed

Description

@caomengxuan666

Summary

Two read-builtin divergences: it reads empty from /dev/fd/N, and it returns success at EOF-without-newline when the redirection is attached to a compound command or an inherited fd — which makes the classic while read loop run one extra iteration. Both confirmed against real GNU bash.

Environment: Niubash 1.4.0 · rubash git e785c3c8f610 — Windows 10 x64.
Method: differential vs GNU bash 5.2.21(1) / 5.3.0.


R3.1 read builtin reads empty from /dev/fd/N

$ printf 'DATA\n' > f ; exec 6< f
$ read -r z < /dev/fd/6 ; echo "[$z]"
niu : []        # empty
bash: [DATA]

Same for /dev/fd/0. External commands on the same path are correct:

$ cat /dev/fd/6
niu : DATA      # works

Writing to /dev/fd/N also works. Only the read builtin's read path is affected.


R3.2 read returns 0 at EOF-without-newline under compound / fd redirection

$ printf 'abc' > f
$ { IFS= read -r l; echo "rc=$?"; } < f
niu : rc=0      bash: rc=1

$ exec 9< f ; IFS= read -r l <&9 ; echo "rc=$?"
niu : rc=0      bash: rc=1

The simple-command form is correct, and so are the pipe form and mapfile:

$ IFS= read -r l < f ; echo "rc=$?"
niu : rc=1      bash: rc=1      # OK

Effect on the most common loop idiom — a file whose last line lacks \n:

$ printf 'abc' > f
$ n=0; while IFS= read -r l; do n=$((n+1)); done < f; echo $n
niu : 1         bash: 0
$ printf 'aaa\nbbb' > g
niu loops 2×    bash loops 1×

The pipe form is unaffected: cat f | while IFS= read -r l; do …; done matches bash.

Activity

  1. added
    bug
    P1-high
    on Oct 17, 2026
  2. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Version note (correction to the Method line): the oracle was reached with bare wsl.exe -e bash, which resolves through PATH to /usr/local/bin/bash = GNU bash 5.3.0(1) — not 5.2.21. The 5.2.21 build lives at /bin/bash, and every item in this report was re-checked on it as well; results were identical on both.

    So the two versions are listed in the wrong order above: 5.3.0 was the default, 5.2.21 the cross-check.

    No finding here changes; noting it only so the numbers reproduce. (Incidentally 5.3.0 is also the oracle cited in rubash#641, "the repository oracle is WSL 5.3.0".)

  3. added
    area:read
    family:S3
    on Oct 17, 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

    P1-higharea:readbugfamily:S3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions