Skip to main content
Although standard output and standard error are already covered in the Shell Scripts for Beginners, we repeat them here because redirection and pipelines introduce subtleties you should understand. Think of program output as streams of data flowing through pipes—commands, scripts, and programs generate streams that go to your terminal. Knowing how to capture, separate, and redirect those streams is essential for diagnosing problems and writing reliable shell scripts. In this lesson we cover what stdout and stderr are, how they behave by default, and how to redirect them to files or to each other.

stdout vs stderr: the basics

A simple command like ls writes its normal results to standard output (stdout). Flags change the format or content of stdout; for example ls -l prints a detailed listing:
If you pass an invalid option (for example ls -j), the program prints an error message to standard error (stderr):
Visually on the terminal both stdout and stderr look identical, but they are different streams and can be handled separately.
A dark-themed diagram titled "Stdout and Stderr" shows a faucet splitting into two small bowls, each dripping a colored drop. The left drop labeled "Stdout" is a clean blue droplet with a check mark, while the right "Stderr" drop is greenish with error-like symbols.
Analogy: imagine two pipes at your sink—one for clean water and one for wastewater. If you can’t tell them apart, you might accidentally use contaminated water. Likewise, if you don’t separate stdout and stderr you may miss errors or mix them with normal output. Keeping them separate helps you diagnose problems. Some programs are intentionally silent on success. For example mv moves or renames files without printing anything to stdout unless you request verbose output:
If mv cannot find the source file, it writes an error to stderr:

Redirection refresher

By default > (greater-than) and >> (append) redirect only stdout. That means error messages sent to stderr are not captured by > unless you explicitly redirect stderr.
A dark-themed slide titled "Stdout and Stderr / Redirection Symbols" showing green icons for the redirection operators ">" (greater than) and ">>" (double greater than) used to separate standard output and error.
Examples with echo and file redirection:
Now try redirecting a command that produces stderr with >:
To capture stderr you must redirect file descriptor 2:
Why 2>? Unix-like processes use numeric file descriptors for standard streams:
  • 0 — standard input (stdin)
  • 1 — standard output (stdout)
  • 2 — standard error (stderr)
You can use these numbers explicitly: 1> redirects stdout; 2> redirects stderr. Examples showing separate files for stdout and stderr:
If the command produces normal output, stdout goes to the file assigned to file descriptor 1:

Merging streams: redirect stderr into stdout (and ordering)

You can send stderr to wherever stdout is currently going by using 2>&1. Similarly, you can redirect stdout to wherever stderr is going with 1>&2. Important: the order of redirections matters. Typically you redirect stdout first, then point stderr at stdout:
If you reverse the order (2>&1 > all.txt) the shell first redirects stderr to the current stdout (usually the terminal) and then redirects stdout to all.txt, leaving stderr still connected to the terminal.
When you want stderr to end up in the same place as stdout, use the pattern: command >file 2>&1. Always redirect stdout first, then redirect stderr to 1.

Quick operator reference

Quick text summary:

Wrap-up

You now know:
  • stdout and stderr are separate streams (fd 1 and fd 2).
  • > and >> affect stdout by default.
  • Use 2> to capture stderr, and 2>&1 to merge stderr into stdout (remember ordering).
  • Properly separating or merging streams helps with debugging and script reliability.
Next, apply these patterns in pipelines and scripts to log output, capture errors, and build robust automation.

Watch Video