Pipes and redirection until they feel obvious
Drill |, >, >>, 2>, 2>&1, and < inside a throwaway directory, plus the GNU vs BSD sed -i trap.
Published here , updated · 6 sources · Resume reading
A pipe (|) sends one command's stdout into the next command's stdin [1]. > writes stdout to a file (overwrite), >> appends, 2> catches stderr, 2>&1 merges stderr into stdout's current destination, and < feeds a file into stdin [2][4]. Drill them in a throwaway directory so you never overwrite a real config.
Quick start: create a throwaway demo directory
Always practice in a temp folder. Do not redirect into ~/.bashrc, /etc, or your project tree:
DEMO=$(mktemp -d /tmp/pipes-demo-XXXX)
cd "$DEMO"
printf 'gamma\nalpha\nbeta\n' > words.txtCaptured on this host: a new directory under /tmp with a three-line words.txt. Cleanup later with rm -rf "$DEMO".
Pipe stdout into the next command
Bash pipelines connect commands left to right [1]. Sort the words file:
cat words.txt | sortCaptured output:
alpha
beta
gammasort never saw the path; it only read stdin that the pipe fed. Prefer sort words.txt when one command can open the file itself. Use a pipe when you are chaining filters that only speak stdin and stdout [1][6].
Overwrite and append with > and >>
echo hello > out.txt
echo world >> out.txt
cat out.txtCaptured output:
hello
worldThe first redirect created or truncated out.txt. The second appended. Run > twice by accident and the second run wipes the first. That is why drills live under /tmp [2].
Capture stderr with 2>
File descriptor 2 is stderr. Teaching comics and the Bash redirections chapter treat 0/1/2 as stdin/stdout/stderr [2][4]:
ls /no/such/path 2> err.txt
cat err.txtCaptured err.txt on this host:
ls: cannot access '/no/such/path': No such file or directoryThe terminal stayed quiet for that failing ls because stderr went to the file. Without 2>, the same message would have printed on your screen and never landed in a log you can grep later.
Merge stderr into stdout with 2>&1
ls /no/such/path > both.txt 2>&1
cat both.txtCaptured both.txt:
ls: cannot access '/no/such/path': No such file or directoryPut 2>&1 after the > so stderr follows stdout into that file [2][4]. Reverse the order (2>&1 > both.txt) and stderr still points at the old stdout (your terminal) while only stdout goes to the file. Order is the whole lesson.
A pipe sends command A stdout into the next stdin. Greater-than, append, and stderr redirects attach streams to files instead. Merge with 2>&1 only after the stdout redirect so descriptor 2 follows descriptor 1 into that path; reverse the order and stderr still hits the terminal.
Feed a file on stdin with <
wc -l < words.txtCaptured output:
3wc never opened the path as an argument; the shell attached the file to stdin [2]. That matters when a tool only reads stdin, or when you want the command's argv free of the filename.
Label GNU vs BSD sed -i before you copy it
Warning: in-place sed edits files. Stay inside $DEMO.
FreeBSD sed documents an empty backup suffix for in-place edits [3]:
sed -i '' -e 's/alpha/ALPHA/' words.txtGNU sed on Linux commonly uses:
sed -i -e 's/alpha/ALPHA/' words.txtCaptured on this GNU sed host after rewriting a copy of the demo file:
gamma
ALPHA
betaOn macOS, prefer the FreeBSD form from the manpage [3]. Prefer printf over echo -e when you care about portability. Paste the wrong -i form across OS lines and you get a backup file you did not want, or a syntax error mid-edit.
Why a live pipe can look stuck
Many programs block-buffer when stdout is a pipe or file, so cmd | grep can look frozen until a buffer fills. Julia Evans documents this buffering gotcha [5]. Line-buffering flags or stdbuf help in some toolchains; check the tool you are using rather than inventing a universal buffer size. If the left side is still computing and the right side has printed nothing, wait or force line buffering before you kill the SSH session.
Mistakes that burn an hour
Redirecting into a real config while "just testing" >. Use $DEMO first.
Writing 2>&1 > file and wondering why errors still hit the terminal. Swap to > file 2>&1 [2].
Treating | as a way to open a file for one program that already accepts a path. Prefer the path argument when it exists.
Copying sed -i from a Linux gist onto macOS without the empty suffix [3].
Blaming the network when a pipe is only waiting on a full stdout buffer [5].
Depth: pipelines versus redirections
A pipe connects two processes. Redirection attaches a process to a file or to another descriptor [1][2][6]. You can combine them: cmd1 2>err.txt | cmd2 >out.txt keeps stderr of cmd1 in err.txt while cmd2 writes the filtered stdout. Read the line left to right the way the shell does: set up redirections for each command, then connect the pipe between them.
When you need process lists and job control around the same pipe, continue with top terminal commands part 2. For the navigation and search set that comes before pipes, start at essential Linux commands part 1. For which modern CLI replaces a classic filter, see CLI tools matched to jobs.
Prove one compound line
Inside $DEMO, rebuild a single line that uses several operators so muscle memory sticks:
printf 'z\na\nm\n' > raw.txt
sort < raw.txt > sorted.txt 2> sort-err.txt
cat sorted.txt
wc -l < sorted.txtYou should see three sorted letters in sorted.txt and a line count of 3. sort-err.txt should be empty on a healthy run. If sort-err.txt grows, read it before you chase the empty sorted.txt.
That compound line is the whole skill: stdin from a file, stdout to another file, stderr parked separately, no pipe required. Add | uniq only when a second process must filter the stream [1][2].
File descriptors without the scare story
You do not need to memorize every number. Remember three: 0 stdin, 1 stdout, 2 stderr [2][4]. 2>&1 means "point 2 at whatever 1 currently points at." After > both.txt, descriptor 1 points at both.txt, so 2>&1 sends stderr there too. Before any >, descriptor 1 is still the terminal, so a leading 2>&1 only duplicates the terminal.
Named pipes (FIFOs) exist for cases where two processes need a pipe that is also a path on disk [6]. Skip them until the six operators above are automatic. Most day-to-day shell work never needs a FIFO.
Append logs without wiping last night's run
Ops scripts often need "add a line, keep history." That is >>, not >. A one-line pattern inside $DEMO:
date >> run.log
echo checkpoint >> run.log
wc -l < run.logEach run grows run.log. Swap to > only when you intentionally want a fresh file. Pair append with a separate 2>> run.err when you want errors in another growing file and stdout clean in the log [2].
Here-documents (<<EOF) feed multi-line stdin without a temp file. They are still redirection, just with the body inline in the script. Learn the six operators first; add here-docs once > and < feel boring.
Pipeline exit status
By default bash reports the exit status of the last command in a pipeline [1]. A failing left side can hide behind a successful right side. set -o pipefail changes that so a failure earlier in the pipe surfaces. Part 2 of the terminal series shows a short pipefail demo when you are ready for job control around the same idea: top terminal commands part 2.
Until then, check the file you redirected into. An empty out.txt plus a loud err.txt is a clearer signal than a pipeline status you forgot to enable.
Depth: six operators in one compound line
Build the drill under /tmp/pipes-demo so you never aim redirects at a real project tree. Create a.txt with three short lines, then practice each operator alone before you combine them. Read the man pages for your shell's redirection rules when the host is macOS or FreeBSD, because BSD sed -i and some builtins diverge from the Linux paste you copied [2][3].
cat a.txt | wc -l moves stdout into the next stdin. cat a.txt > out.txt replaces out.txt. cat a.txt >> out.txt appends. cat missing 2> err.txt parks stderr alone. cat missing > out.txt 2>&1 merges stderr into the already redirected stdout file; reverse that order and stderr still hits the terminal while stdout alone fills the file. wc -l < a.txt feeds stdin from a path without a left-hand producer.
Order is the trap that burns production logs: open the target file with > or >> first, then attach 2>&1 so descriptor 2 follows descriptor 1 to that path. Confirm with ls -l out.txt err.txt and cat both files after each attempt. When a long pipeline looks empty while CPU is busy, suspect block buffering on the producer; force line buffering where your libc supports stdbuf, or print with an explicit flush, then re-run the same drill until the consumer prints line by line.
A second trap is discarding stderr while debugging. Keep 2> err.txt on a named file during the drill, then open it before you declare the pipeline healthy. A third trap is writing into the current directory by habit. Stay inside /tmp/pipes-demo until the six operators are muscle memory, then move the pattern into a real repo with a dry-run path.
Keep this page on the six operators and file descriptors 0, 1, and 2. Broader find and grep drills stay on essential Linux commands part 1. Process and job control stay on part 2. Tool picking for search stays on CLI tools matched to jobs.
Depth: why 2>&1 order is not a meme
Descriptors are numbers the process already owns. When you write > out.txt, the shell opens that path and points descriptor 1 at it. When you then write 2>&1, descriptor 2 is pointed at whatever descriptor 1 currently targets. If you reverse the tokens, descriptor 2 still points at the terminal, then descriptor 1 alone moves to the file. That is why the "wrong order" demo is worth running once with your eyes on both files [1][2].
After the drill, delete /tmp/pipes-demo or leave it as a throwaway lab. Do not promote the folder into a project root. The skill is reading streams, not collecting sample text files.
Browse the Dev explainers topic.
Send this to the friend who still guesses what 2>&1 means every time they copy it.

Keep this guide
Filed under Dev explainers, Dev tools
Questions about this guide
What does a pipe do?
A pipe sends the standard output of the left command into the standard input of the right command. Bash documents pipelines in its manual.
What is the difference between > and >>?
> truncates or creates the file and writes stdout there. >> appends stdout to the file.
Why does 2>&1 order matter?
2>&1 sends stderr to wherever stdout currently points. Put it after a > redirect when you want both streams in that file.
Why does sed -i look different on macOS?
FreeBSD sed (also common on macOS) documents sed -i '' -e '...'. GNU sed on Linux accepts sed -i -e '...'. Label the OS before you copy a snippet.
Sources
- FreeBSD sed(1), accessed 2026-11-20man.freebsd.org
- Julia Evans: bash redirects comic, accessed 2026-11-20wizardzines.com