Skip to content

bug: result=$(cmd 2>&1 >file) redirect ordering incorrect #853

@chaliy

Description

@chaliy

Problem

The redirect combination 2>&1 >file inside command substitution doesn't follow bash's left-to-right evaluation order.

Expected behavior (real bash):

  1. 2>&1 — stderr is redirected to where stdout currently points (the $() capture pipe)
  2. >file — stdout is redirected to the file

So result=$(cmd 2>&1 >file) should capture stderr in result and write stdout to file.

Actual behavior (bashkit): Both stdout and stderr go to the file; result is empty.

Reproduction

f() { echo "stdout"; echo "stderr" >&2; }
result=$(f 2>&1 >"/tmp/out.txt")
echo "result=[$result]"
cat /tmp/out.txt

Real bash: result=[stderr], file contains stdout
Bashkit: result=[], file contains stdout\nstderr

Impact

The wedow/ticket cmd_link uses this pattern to capture awk's stderr count output while writing the modified file to a temp file:

result=$(awk '...' "$file" 2>&1 >"${file}.tmp")
# result should get the count from: END { printf "%d", added > "/dev/stderr" }
# file.tmp should get the modified file content

With bashkit, result is always empty, so ((count += result)) fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions