>_How the Terminal Works · Lesson 7 of 7
Terminal Cheatsheet
The mental model plus the keystrokes, on one page.
Text
── The model ────────────────────────────
terminal emulator: draws text, sends keystrokes
shell (bash/zsh): parses commands, runs programs
they're separate programs — either swaps out
pressing Enter:
1. shell expands: $VARS *.glob ~ $(cmd)
2. finds program: aliases -> builtins -> $PATH dirs
3. fork + exec, wires up the 3 streams
4. waits; exit code lands in $?
streams: 0=stdin 1=stdout 2=stderr
cmd > f stdout to file
cmd 2> f stderr to file
cmd > f 2>&1 both
cmd1 | cmd2 stdout -> next stdin
cmd && next only on success
environment: KEY=value copied parent -> child
export makes a var inheritable; never flows back
startup files: ~/.bashrc / ~/.zshrc (PATH, aliases)
escape codes: ESC[32m = green; cursor moves; \r redraw
vim/htop = raw mode + furious redrawingBash
# ── Keystrokes that matter ──────────────
# Ctrl+C kill current program (SIGINT)
# Ctrl+Z suspend (fg to resume, bg to background)
# Ctrl+D end of input / close shell
# Ctrl+R search command history
# Ctrl+A/E start/end of line Ctrl+W delete word
# Tab complete Tab Tab show options
# !! last command !$ its last argument
# ── Diagnosis one-liners ────────────────
type CMD # alias? builtin? which file?
echo $PATH # where commands are searched
echo $? # did the last command succeed?
env | sort # my environment
history | tail -20
stty sane # fix a garbled terminal (+ Enter)
# ── Jobs ────────────────────────────────
long_task & # start in background
jobs # list them
fg %1; bg %1 # foreground / background
nohup task & # survives closing the terminal
# ── SSH & remote ────────────────────────
ssh user@host # remote shell
ssh-keygen -t ed25519 # make keys (once)
ssh-copy-id user@host # passwordless login
ssh user@host 'df -h' # one remote command
scp file user@host:/path/ # copy over ssh
rsync -av src/ user@host:dst/ # sync, resumable
tmux # persistent session
# Ctrl+B D to detach; tmux attach to resume