Skip to content

redirection & fd: 2>&1 >file order, builtin write-error routing, closed-stderr status, subshell-uncaptured cd error, bare-cat same-file #699

Closed

Description

@caomengxuan666

Summary

Five redirection / file-descriptor divergences from GNU bash, all with minimal repros and confirmed on the real bash over repeated runs.

Environment: Niubash 1.4.0 · rubash git e785c3c8f610 · winuxcmd 1.1.12 — Windows 10 x64 (build 19045).
Method: differential run against GNU bash 5.2.21(1) (cross-checked on 5.3.0); each item below is a minimal deterministic repro verified on both shells.


R1.1 2>&1 >file applies redirections in the wrong order

$ { echo OUT; echo ERR >&2; } 2>&1 >f ; echo "file=[$(cat f)]"
niu : file=[OUT
ERR]        ; nothing on stderr
bash: file=[OUT]      ; ERR goes to the original stdout

Real impact — the common idiom cmd 2>&1 >/dev/null:

$ f(){ echo OUT; echo ERR >&2; }; f 2>&1 >/dev/null
niu : (prints nothing — stderr wrongly discarded)
bash: ERR

Same defect with 1>&2 2>file (both land in the file). The safe order >file 2>&1 is correct.


R1.2 Builtin write errors are not sent to the command's redirected stderr

$ printf 'ro\n' > f; exec 4< f
$ echo X >&4 2>/tmp/err ; wc -c < /tmp/err
niu : 0        # diagnostic went to the shell's own stderr instead
bash: 60

R1.3 Closing stderr corrupts an applet's exit status and emits a spurious diagnostic

$ ls /nonexistent 2>&- ; echo $?
niu : "ls: write error: Bad file descriptor" (on the closed fd's real stderr) ; rc=1
bash: (silent) ; rc=2

$ cat /nonexistent 2>&- ; echo $?
niu : "cat: write error: Bad file descriptor" ; rc=1
bash: (silent) ; rc=1

Two problems: the diagnostic is emitted even though fd 2 was closed, and ls's status is 1 instead of 2.


R1.4 cd diagnostics escape stderr redirection when run in a subshell

$ ( cd /no_such_zzz ) 2>e ; wc -c < e
niu : 0        # message bypasses the shell's fd table, goes to real stderr
bash: 69

$ ( cd /no_such_zzz ) 2>&1 | wc -l
niu : 0        bash: 1

cd /no_such_zzz 2>e (not in a subshell) is captured, and { cd …; } 2>&1 is captured. Other builtins (source, set -o bad, umask, read -u) and external commands are captured correctly. Specific to the directory-changing builtins.


R1.5 bare cat fast path changes cat FILE > FILE / >> semantics

$ printf 'hello\nworld\n' > f ; cat f > f ; wc -c < f
niu : 12   (file unchanged)
bash: 0    (truncated; bash also prints "cat: input file is output file")

$ printf 'hello\nworld\n' > f ; cat f >> f ; wc -c < f
niu : 24   (doubled)
bash: 12

Only bare cat is affected. command cat, and python/od/cut/sort/grep/sed self-redirects, all truncate like bash. Likely related to the same inline fast path noted in unixwin/niubash#383.

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. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Status: a fix PR for all five findings is in flight (reproduction-first, 2>&1 >file ordering as the headline item, same review bar as the other current rubash PRs). It will reference this issue in the body — leaving this note so nobody starts it in parallel. If you plan to rebalance, redirect instead.

  4. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Verification status against this issue, from PR #725 (fix/699-redirection-order, oracle: WSL bash 5.3.0 cross-checked on Git bash 5.2.37):

    • R1.1a/c, R1.2, R1.3a/b, R1.4a/b — all reproduce on the installed niu (rubash e785c3c8f610) and on origin/master, and all are fixed by PR fix(executor): apply redirections left to right (#699) #725.
    • R1.1b (f(){…} 2>&1 >f; call-site) — does not reproduce on either base; call-site function redirects are already ordered correctly (tested byte-identical).
    • R1.5 (bare cat f > f / >>) — does not reproduce on either base: { cat f > f; } truncates (0 of original bytes gone), cat f >> f appends (12/12), i.e. the output/operand identity handling is already correct. The only observable difference vs GNU is a missing cat: f: input file is output file warning (rc behavior fine). The numbers quoted in the report (0/12 divergences, "left"/"right" mismatch) were not observable here on either binary.

    If your R1.5 measurement came from a differently-built binary (older tag / different toolchain), please state which — that would make it a build-specific defect rather than a current one.

  5. added
    area:fd
    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:fdbugfamily:S3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions