Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / The Linux Command Line

    Pipes and Redirection

    > truncates the file before the command even runs. sort data.txt > data.txt gives you an empty file: the

    Chapter 5 of 125 min readOften asked in interviews

    This chapter is what makes the command line powerful.

    The Plain Picture#

    A factory line: one machine cuts, the next paints, the next packs. Each machine does one simple job, and a conveyor belt carries the output of one straight into the next. By arranging simple machines differently, you can build almost any product.

    The Real Names#

    Every process has three standard streams (Operating Systems Ch 10): stdin (0), stdout (1), stderr (2). By default all three connect to your terminal. The shell can reconnect them:

    SyntaxMeaning
    cmd > filesend stdout to a file (overwrite it)
    cmd >> filesend stdout to a file (append to the end)
    cmd 2> errors.txtsend stderr to a file
    cmd > out.txt 2>&1send stdout to a file, and stderr to the same place as stdout
    cmd < input.txtread stdin from a file
    cmd1 | cmd2a pipe: stdout of cmd1 becomes stdin of cmd2 — the conveyor belt
    cmd > /dev/nullthrow output away (/dev/null is a device that discards everything written to it)

    The Unix idea behind it: small tools that each do one thing well, and read and write plain text, so any tool can feed any other.

    Under the Hood#

    A pipe is a small buffer in the kernel. Both commands run at the same time as separate processes; the first writes into the buffer, the second reads from it. If the reader is slow, the buffer fills and the writer is paused (blocked, Operating Systems Ch 3) — so pipes work even on files far bigger than RAM.

      ┌────────┐ stdout   ┌─────────────┐   stdin ┌────────┐ stdout   ┌──────────┐  stdin ┌────────┐
      │  cat   │ ───────▶ │ kernel pipe │ ──────▶ │  grep  │ ───────▶ │   pipe   │ ─────▶ │  wc -l │ ──▶ terminal
      └────────┘          └─────────────┘         └────────┘          └──────────┘        └────────┘

    Exit codes join commands#

    Every command exits with a code: 0 = success (Operating Systems Ch 12). The shell uses it:

    Terminal
    echo $?                              # the exit code of the last command
    mkdir build && cd build              # && : run the second only if the first SUCCEEDED
    cd /nonexistent || echo "no such dir"   # || : run the second only if the first FAILED
    cmd1 ; cmd2                          # ; : run both, regardless

    Gotchas

    • > truncates the file before the command even runs. sort data.txt > data.txt gives you an empty file: the shell emptied data.txt before sort read it. Write to a new file, then mv it.

    • The order in > file 2>&1 matters. 2>&1 > file sends stderr to the terminal (where stdout was at that moment) and only stdout to the file.

    • A pipeline's exit code is the last command's by default, so failures early in a pipe are hidden. Scripts fix this with set -o pipefail (Ch 9).

    Check yourself

    5 questions
    1

    > vs >>?

    Show answer

    > overwrites the file; >> appends.

    2

    How do you run a command and save both its normal output and its errors to run.log?

    Show answer

    cmd > run.log 2>&1.

    3

    What does | do?

    Show answer

    Connects the first command's stdout to the second command's stdin.

    4

    What does make build && ./deploy.sh guarantee?

    Show answer

    ./deploy.sh runs only if make build succeeded (exit code 0).

    5

    Why does sort names.txt > names.txt destroy the file?

    Show answer

    The shell truncates names.txt to empty before sort reads it.