DEV Community

Bash redirection explained: >, >>, 2>, 2>&1 and | (and why the order matters)

AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (GNU bash 5.2.37); all outputs below are copied from that run, not written by hand.

Why do errors still show up on your screen after > file? This video explains the two output streams, stdout and stderr, and shows live what >, >>, 2>, 2>/dev/null, 2>&1, &> and | do, why 2>&1 > file doesn't work, how to save output and still see it with tee, and how noclobber protects files from an accidental >.

The problem

You redirect a command into a file, and errors still appear on your screen. Or a log file is empty when you expected everything to be in it.

Two output streams

Here's a command that produces both normal output and an error: we list a folder that exists, and one that doesn't.

$ ls logs missing
ls: cannot access 'missing': No such file or directory
logs: a.log

Both lines land on the screen, but they travel through different channels. The listing goes to standard output, stream number one. The error goes to standard error, stream number two.

> sends stdout to a file

> redirects standard output into a file.

$ ls logs missing > out.txt
ls: cannot access 'missing': No such file or directory

The error still appeared, because only stream one went into the file. Let's check what the file got.

$ cat out.txt
logs: a.log

Just the listing. This is why error messages keep showing up even after you redirect.

2> sends stderr to a file

To catch the errors, put the stream number in front: 2>.

$ ls logs missing 2> err.txt
logs: a.log
$ cat err.txt
ls: cannot access 'missing': No such file or directory

Now the listing stays on screen and the error is in the file. And if you just want errors to disappear, send them to /dev/null, a special file that throws away everything written to it.

$ ls logs missing 2>/dev/null
logs: a.log

2>&1: both streams into one file

To get everything in one file, redirect standard output to the file, and then send stream two to wherever stream one is going. That's 2>&1.

$ ls logs missing > all.txt 2>&1
$ cat all.txt
ls: cannot access 'missing': No such file or directory
logs: a.log

Both lines are in the file. Bash also has a shorter form, &>, which does the same thing.

Now the classic mistake: the same parts, in the other order.

$ ls logs missing 2>&1 > wrong.txt
ls: cannot access 'missing': No such file or directory

The error is on the screen again. Bash reads redirections from left to right. When it sees 2>&1, stream one still points to the terminal, so stream two is copied to the terminal. Only after that is stream one moved to the file.

$ cat wrong.txt
logs: a.log

So the rule is: file first, then 2>&1.

> overwrites, >> appends

One > replaces the whole file. Two of them add to the end.

$ echo first > notes.txt && echo second > notes.txt && cat notes.txt
second

First is gone, overwritten by second. Now with two arrows.

$ echo third >> notes.txt && cat notes.txt
second
third

For log files you almost always want >>. One wrong arrow can wipe a file. Bash can protect you: with noclobber switched on, a single > refuses to overwrite an existing file.

$ set -o noclobber && echo oops > notes.txt
/tmp/.sp_seg.sh: line 8: notes.txt: cannot overwrite existing file
$ set +o noclobber

Pipes only carry stdout

The pipe sends standard output of one command into the next one. Let's count lines with wc -l.

$ ls logs missing | wc -l
ls: cannot access 'missing': No such file or directory
2

The error skipped the pipe and went straight to the screen, so only two lines were counted. Add 2>&1 before the pipe to send both streams through.

$ ls logs missing 2>&1 | wc -l
3

tee: save it and see it

Sometimes you want the output on screen and in a file. tee does both: it writes what it receives to a file and passes it on.

$ ls logs missing 2>&1 | tee run.log
ls: cannot access 'missing': No such file or directory
logs: a.log
$ cat run.log
ls: cannot access 'missing': No such file or directory
logs: a.log

Use tee -a if you want to append instead of overwrite.

The trap, in one paragraph

Which arrow adds to the end of a log file instead of wiping it: > or >>? Two arrows append. A single arrow replaces the whole file.

Quick check

  1. I want stdout and stderr in one file. Is it cmd 2>&1 > all.txt or cmd > all.txt 2>&1?

    • cmd 2>&1 > all.txt
    • cmd > all.txt 2>&1

    Answer cmd > all.txt 2>&1: Redirections run left to right. Point stream one at the file first, then copy stream two there.

  2. Which arrow adds to the end of a log file instead of wiping it: > or >>?

    • >
    • >>

    Answer >>: Two arrows append. A single arrow replaces the whole file.

Recap

Programs have two outputs: stream one for results, stream two for errors. > and the pipe only take stream one. 2> catches errors, /dev/null throws them away, and > file 2>&1 puts everything in one file, in that order. >> appends, and tee lets you watch and save at once.

Source and reproducible setup

This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.