Master Linux I/O Redirection: stdout, stderr, and /dev/null Explained
This guide explains Linux command I/O redirection, covering how to redirect stdout and stderr, combine streams, use /dev/null, and the importance of ordering when chaining redirections.
Basic redirection syntax
$ command > file 2>&1
$ command >> file 2>&1File descriptor numbers: 1 = stdout, 2 = stderr, 0 = stdin. The ampersand ( &) after a redirection operator tells the shell that the target is a descriptor, not a regular file. Thus 2>1 writes stderr to a file named 1, while 2>&1 sends stderr to the same destination as stdout.
Shorthand for both streams
The construct >file (or >&file) redirects both stdout and stderr to file, equivalent to >file 2>&1.
Order of redirections
Redirections are evaluated left‑to‑right. Example:
find /etc -name .bashrc > list 2>&1 # stdout → list, then stderr → same list
find /etc -name .bashrc 2>&1 > list # stderr → original stdout, then stdout → list (stderr stays on original stdout)Input redirection
command1 < file1Discarding output
Redirect to /dev/null to discard data:
command > /dev/null # discard stdout
command > /dev/null 2>&1 # discard both stdout and stderrCommon redirection patterns
Redirect stdout only: command > file Append stdout: command >> file Redirect stderr only: command 2> file Append stderr: command 2>> file Merge stderr into stdout: command > file 2>&1 or command >> file 2>&1 Redirect stdin and stdout:
command < in.txt > out.txtWhy merge stderr into stdout?
stderr is unbuffered while stdout is buffered; merging ensures the relative order of messages when both streams are captured.
Reference of redirection operators
command > file– redirect stdout to
file command < file– redirect stdin from
file command >> file– append stdout to
file n > file– redirect descriptor n to
file n >> file– append descriptor n to
file n >& m– make descriptor n a copy of descriptor m (e.g., 2>&1) n <& m – duplicate input descriptor m onto
n << TAG– here‑document: treat lines up to TAG as stdin
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
