Lesson 06 · tested on Debian 13
Bash redirection explained: >, >>, 2>, 2>&1 and | (and why the order matters)
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 >.
Video coming soon — @ShellProven
Commands
Exactly as run in the video. Full folder with setup script and log: terminal-tutorials/bash-redirection.
# Two output streams
$ ls logs missing
ls: cannot access 'missing': No such file or directory
logs:
a.log
# > sends stdout to a file
$ ls logs missing > out.txt
ls: cannot access 'missing': No such file or directory
$ cat out.txt
logs:
a.log
# 2> sends stderr to a file
$ ls logs missing 2> err.txt
logs:
a.log
$ cat err.txt
ls: cannot access 'missing': No such file or directory
$ ls logs missing 2>/dev/null
logs:
a.log
# 2>&1: both streams into one file
$ ls logs missing > all.txt 2>&1
$ cat all.txt
ls: cannot access 'missing': No such file or directory
logs:
a.log
$ ls logs missing 2>&1 > wrong.txt
ls: cannot access 'missing': No such file or directory
$ cat wrong.txt
logs:
a.log
# > overwrites, >> appends
$ echo first > notes.txt && echo second > notes.txt && cat notes.txt
second
$ echo third >> notes.txt && cat notes.txt
second
third
$ 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
$ ls logs missing | wc -l
ls: cannot access 'missing': No such file or directory
2
$ ls logs missing 2>&1 | wc -l
3
# tee: save it and see it
$ 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
Quick check
Same two questions as in the video. Checked here, in your browser — nothing is sent anywhere.
Transparency. Script and narration made with AI, human-reviewed. The robot and the dog are AI-generated cartoon characters. The commands and their output are real runs in a container.