Skip to main content
In this lesson we’ll cover input and output redirection in Linux — how to route data into and out of programs using the shell. These techniques let you save program output to files, separate error messages from normal output, feed files as program input, and connect small utilities together with pipes.

Example: sort reads stdin and writes stdout

Many Unix utilities follow the convention of reading from standard input (stdin) and writing to standard output (stdout). For example, sort reads lines of text and writes them in order. Suppose file.txt contains:
Running sort file.txt reads file.txt and prints sorted lines to the terminal (stdout):
To save that sorted output to a file instead of printing to the terminal, use output redirection with >:
If the target file does not exist, > creates it. By default > overwrites the file; use >> to append instead:
The forms > and 1> are equivalent because file descriptor 1 is stdout:

Standard streams and file descriptors

Programs use three standard streams by default:
  • Standard Input (stdin) — file descriptor 0
  • Standard Output (stdout) — file descriptor 1
  • Standard Error (stderr) — file descriptor 2
Use < to redirect standard input, > (or 1>) to redirect standard output, and 2> to redirect standard error.
A diagram illustrating Unix standard streams: file.txt is fed as stdin into the sort command, the command’s stdout goes to the terminal (1>), and stderr is redirected to errors.txt (2>).

Redirecting stderr (error messages)

Many commands print error messages to stderr (fd 2). For example:
If you want to discard error messages (so they don’t clutter the terminal), redirect stderr to /dev/null:
/dev/null discards anything written to it — useful when you want to ignore unwanted output such as diagnostic or permission errors.
You can redirect stdout and stderr to separate files — either overwrite or append:

Capture both stdout and stderr into a single file

To capture both streams into the same file, redirect stdout first, then point stderr at stdout using 2>&1:
Explanation:
  • >all_output.txt redirects stdout (fd 1) to all_output.txt
  • 2>&1 directs stderr (fd 2) to wherever fd 1 currently points (the file)
Order matters when combining redirections. Use >file 2>&1 (or 1>file 2>&1) to ensure both stdout and stderr go to file. Placing 2>&1 before >file will leave stderr pointing at the terminal.
Incorrect ordering example (does not capture stderr into the file):
Safer, clearer alternatives:

Redirecting stdin (feeding input)

Redirecting input is less common but useful when a program reads from stdin (fd 0). Use < to feed a file as input:
This makes the program read emailcontent.txt as if the content were typed at the keyboard.

Here-documents (multi-line stdin)

A here-document (here-doc) embeds multi-line input directly in the command:
EOF is a common delimiter; any marker works. All text between the start and the delimiter is passed to the program’s stdin.

Here-strings (single-line stdin)

A here-string passes a single line/string as stdin using <<<:
Here-docs and here-strings are handy for scripting or automating programs that expect keyboard input.

Pipes: chain small programs

Pipes (|) let you chain utilities so the stdout of one becomes the stdin of the next. Example: remove comment lines, sort the entries, and align columns for readability:
Pipes are powerful for composing small, focused tools (the Unix philosophy) without creating temporary files.

Quick reference

Further reading

Summary
  • Use >/>> for stdout redirection and 2>/2>> for stderr.
  • Use 2>&1 to combine stderr with stdout (mind the order).
  • Use <, here-docs (<<), and here-strings (<<<) to feed stdin.
  • Use pipes (|) to connect programs without intermediate files.

Watch Video

Practice Lab