Skip to content

fix(redirect): fd3 redirection pattern 3>&1 >file sends fd3 to file instead of original stdout #1115

@chaliy

Description

@chaliy

Summary

The compound redirection pattern { ... } 3>&1 >"$file" does not correctly separate fd3 (→ original stdout) from fd1 (→ file). Content written to fd3 via echo "msg" 1>&3 ends up in the file instead of on the terminal.

Reproduction

{
    echo "progress" 1>&3
    echo "file content"
} 3>&1 > /tmp/test_fd.txt

cat /tmp/test_fd.txt
# Expected: only "file content"
# Actual: both "progress" and "file content" appear in the file

Context

Discovered while running bashblog via bashkit CLI. Bashblog uses this pattern in all_posts(), rebuild_index(), all_tags(), and make_rss() to show progress dots on the terminal while writing content to a file:

{
    echo -n "." 1>&3          # progress dot → terminal
    echo "<html>...</html>"   # content → file
} 3>&1 >"$contentfile"

Expected behavior

In { ... } 3>&1 >file:

  1. >file redirects fd1 to the file
  2. 3>&1 duplicates the original fd1 (terminal) to fd3
  3. Writing to fd3 via 1>&3 should go to the terminal, not the file

The fd3 dup should capture the original stdout before the >file redirect is applied.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions