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
| Tool | What It Does |
|---|---|
stdin-stdout-stderr | The 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. |
tee | Split a stream so it goes to both a file and the screen. |
/dev/null | A 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:
| Topic | What It Covers |
|---|---|
| Environment Variables | Variables the shell carries, exporting them, and reading them. |
$PATH | Where the shell looks for commands when you type a name. |
| Aliases | Short names you define for longer commands. |
| History | Remembering and reusing every command you’ve run. |
| Shell Scripting | Turning a series of commands into a reusable script. |
| bash / sh / zsh | The 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.