Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pipes, Redirection & Shell Basics

Every command you’ve learned so far — ls, cat, grep — reads input from somewhere and writes output somewhere. By default, that somewhere is the keyboard and the terminal. This section teaches you to change that: to reroute a command’s output into a file, feed one command’s results directly into another, and capture or discard streams at will.

Pipes and redirection are the glue of the command line. Individually, commands do one small thing; chained together with | and redirected with >, they form workflows that solve real problems. Almost every powerful one-liner in the Linux world is built from these two ideas, so this section is where the command line starts to feel like a programming environment.

The Three Streams

Before anything else, understand that every process has up to three open data streams:

  • stdin (standard input, file descriptor 0) — where a command reads from.
  • stdout (standard output, file descriptor 1) — where a command writes its results.
  • stderr (standard error, file descriptor 2) — where a command writes its error messages, kept separate from results so the two never mix.

Redirection and pipes are simply operators that rewire these streams.

Common Pipes & Redirection Tools

ToolWhat It Does
stdin-stdout-stderrThe three streams, file descriptors, and how to see them.
> / >>Send a command’s stdout to a file, replacing or appending.
2>Send only a command’s error messages to a file.
|Connect one command’s stdout straight into another’s stdin.
teeSplit a stream so it goes to both a file and the screen.
/dev/nullA special file that discards everything written to it.

Shell Basics

Pipes and redirection are only useful once you understand the environment the shell runs in. These six pages cover the rest of the essentials:

TopicWhat It Covers
Environment VariablesVariables the shell carries, exporting them, and reading them.
$PATHWhere the shell looks for commands when you type a name.
AliasesShort names you define for longer commands.
HistoryRemembering and reusing every command you’ve run.
Shell ScriptingTurning a series of commands into a reusable script.
bash / sh / zshThe shell families you’ll actually meet, and how they differ.

Why This Matters

  • You stop losing output. Anything worth keeping gets saved to a file instead of vanishing into scrollback.
  • You stop mixing errors into results. Separating stderr keeps data files clean.
  • Commands compose. The ability to chain tools is the single most force-multiplying skill on Linux — see the Linux Basics section for the broader picture.

A command does one thing; a pipeline does a job. Once you can wire streams together, every command becomes a building block.