Pipes and redirection: where command output goes
A pipe connects one command's stdout to the next command's stdin; redirection decides where stdout and stderr go.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-27-A: redirection order changes where stderr goes; a plain pipe carries stdout only
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
bash -c 'echo out; echo err >&2' >a.txt 2>&1; echo a_out=$(grep -c out a.txt) a_err=$(grep -c err a.txt); bash -c 'echo out; echo err >&2' 2>&1 >b.txt | grep -c err > c.txt; echo c_err=$(cat c.txt) b_err=$(grep -c err b.txt); bash -c 'echo out; echo err >&2' 2>/dev/null | grep -c err; echo doneRecorded output:
a_out=1 a_err=1
c_err=1 b_err=0
0
doneThis run checks the exit status and records the output; it asserts no expected text.
TE-27-B: > overwrites a file and >> appends to it
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
echo hi > o.txt; echo more >> o.txt; cat o.txt; echo new > o.txt; cat o.txtRecorded output:
hi
more
newExit status: 0. This status and the output match what was written before the run.
TE-27-C: expected non-zero exit: a failing command still creates the empty output file, and the exit status is the command's
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
cat nosuch.txt > o.txt; rc=$?; echo size=$(wc -c < o.txt); exit $rcRecorded output:
size=0Standard error from the same run:
cat: nosuch.txt: No such file or directoryExit status: 1. This status and the output match what was written before the run.
Reading the recorded output
In the first command, stdout and stderr both went to a.txt, and the counts a_out=1 and a_err=1 show that a.txt holds one out line and one err line. The second command used the redirections in the other order. There the err line reached the pipe, so c_err=1, and b.txt did not get it, so b_err=0. In the third command stderr went to /dev/null before the pipe, so grep -c printed 0. The word done is the last line.
TE-27-B: the first echo created o.txt with hi, >> added more, and the next > replaced the file with new. The first cat printed hi and more. After the last > the second cat printed new. TE-27-C: cat nosuch.txt > o.txt exited with status 1 and printed cat: nosuch.txt: No such file or directory to standard error. The file o.txt was still created, with size 0.
Limits
A plain pipe carries stdout only; redirection order changes the result; the terminal is only the default in an interactive session.
Source
Evidence
TE-27-A: redirection order changes where stderr goes; a plain pipe carries stdout only
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-27-A | redirection order changes where stderr goes; a plain pipe carries stdout only | 0 | PASS | 55d8a0a99f68 | | TE-27-B | > overwrites a file and >> appends to it | 0 | PASS | 085b57607648 | | TE-27-C | expected non-zero exit: a failing command still creates the empty output file, and the exit status is the command's | 1 | PASS | ba68396451c4 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides