Skip to main content
RunBook Academy

LinuxII · Shell and Command-Line OperationsPipelines and redirection

Redirection, pipes, and three-channel I/O

Foundation⏱ ~12 min🧪 Lab requiredbashechocatteexargs

What you'll learn

  • Redirect stdin, stdout, and stderr to and from files
  • Build pipelines with `|` and process substitution
  • Use /dev/null, here-documents, and here-strings correctly
  • Distinguish destructive overwriting from appending redirection

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

Redirection and pipes are what turn a collection of small commands into a production-grade text-processing toolkit. Most senior sysadmins can read a pipeline the way other engineers read code.

File descriptors

Every process starts with three open file descriptors inherited from its parent:

NumberNameDefaultConvention
0stdinterminal keyboardInput
1stdoutterminal displayNormal output
2stderrterminal displayDiagnostics

You can redirect any of them with the shell’s redirection operators.

Redirecting to files

Configuration changestdout to file (overwrite)
$ echo hello > out.txt; cat out.txt
hello
Configuration changestdout to file (append)
$ echo again >> out.txt; cat out.txt
hello
again
Configuration changestderr to file
$ ls /nonexistent 2> err.txt; cat err.txt
ls: cannot access '/nonexistent': No such file or directory

Illustrative output

Configuration changestdout and stderr to separate files
$ ls /etc/passwd /nonexistent > out.txt 2> err.txt; cat out.txt; echo ---; cat err.txt
/etc/passwd
---
ls: cannot access /nonexistent: No such file or directory

Illustrative output

Pipelines

A pipeline chains commands by feeding each command’s stdout into the next command’s stdin.

Read-only / Safebasic pipeline
$ ps -eo pid,comm | head -10
    PID COMM
1 systemd
124 systemd-journald
135 systemd-udevd
...

Illustrative output

Pipelines have one important subtlety for production scripts: the exit status of a pipeline is the exit status of the last command, not the first. If cmd1 | cmd2 succeeds at cmd1 but cmd2 fails, the pipeline as a whole reports cmd2’s failure — which is what you want.

But if any earlier command in the pipeline fails, the failure is silently lost unless you enable pipefail (covered in the scripting lesson):

Read-only / Safepipefail
$ false | true; echo "pipeline exit: $?"; set -o pipefail; false | true; echo "with pipefail: $?"
pipeline exit: 0
with pipefail: 1

Illustrative output

/dev/null and the black hole

/dev/null is a device file that accepts any input and produces no output. Two common uses:

Configuration changesilence everything
$ command > /dev/null 2>&1
Read-only / Safeempty input
$ command < /dev/null

Here-documents and here-strings

A here-document lets you embed a block of text as stdin to a command:

Configuration changehere-doc
$ cat <<'EOF' > config.txt
setting_one=alpha
setting_two=beta with spaces
EOF
cat config.txt
setting_one=alpha
setting_two=beta with spaces

Illustrative output

A here-string is the one-line version:

Read-only / Safehere-string
$ bc <<< "2 + 2"
4

Illustrative output

tee — the fork in the pipeline

tee reads stdin and writes it to both stdout and one or more files. This is the canonical way to capture intermediate pipeline state without breaking the chain:

Configuration changetee in a pipeline
$ journalctl -u ssh --since "1 hour ago" | tee /tmp/ssh.log | grep -i 'failed' | head -20
...

Illustrative output

WhyThisMatters

Knowledge check

Knowledge check · 3 questions

  1. Q1. In `command > out.txt 2>&1`, which streams end up in out.txt?

  2. Q2. A pipeline `cmd1 | cmd2` exits with the status of cmd1 by default.

  3. Q3. Which of the following are safe patterns in production shell scripts? Select all that apply.

Passing score: 75%. Answers are checked in this browser.