Separate the two output streams

short · 20 min · Objective 1.5

Task

Prove to yourself, with commands whose result you can check, that stdout and stderr are separate channels, that redirection order decides what is captured, and that a pipe carries only one of them. Every one of these is a scenario the exam presents as a puzzle, and each becomes obvious once you have watched it happen.

Steps

  1. Run a command that writes to both channels at once: ls /etc /nonexistent. Note that both appear on your terminal, mixed.
  2. Send only stdout to a file: ls /etc /nonexistent > out.txt. The error remains on screen. Inspect out.txt.
  3. Send only stderr to a file: ls /etc /nonexistent 2> err.txt. Now the listing is on screen and the error is not.
  4. Capture both, correctly: ls /etc /nonexistent > both.txt 2>&1.
  5. Capture both, incorrectly: ls /etc /nonexistent 2>&1 > wrong.txt. Compare the two files and explain the difference before reading on.
  6. Demonstrate that a pipe carries stdout only: ls /etc /nonexistent | wc -l -- the error still reaches your terminal and is not counted.
  7. Create a file with content, then destroy it with >: date > log.txt, then cat missing.txt > log.txt. Check the size of log.txt afterwards.

Verify

cd ~/lab-redirect
grep -c . both.txt      # contains the listing AND the error
grep -c . wrong.txt     # contains the listing only
grep -ci 'no such file' both.txt   # 1
grep -ci 'no such file' wrong.txt  # 0
stat -c '%s' log.txt    # 0 -- truncated before cat ever ran

both.txt must contain the error message and wrong.txt must not. If they are identical, the redirections ran in the same order and you have swapped something -- read the two command lines again, left to right.

Notes

Step 7 is the one worth internalising. The shell opens and truncates the target before the command starts, so a failing command still destroys the file. That is why > on a log you care about is a one-way operation, and why >> exists.