Shell history is a real DRY tool, not trivia

Your shell already remembers everything you typed — bang syntax lets you reuse it instead of retyping

Event designators (!!, !$, !:2, ^foo^bar) and the POSIX fc command turn shell history into an editable, reusable resource — old, powerful, and mostly forgotten

Core claimhigh — this is the article's stated thesis, not a contested claim

Event designators and fc are not obscure trivia — they are a legitimate DRY practice for the command line, hiding since the late 1970s in bash, csh, tcsh and zsh (POSIX shells get fc instead). Learning a handful of them replaces arrow-key-mashing and retyping with direct references to previous commands and their parts.

The pattern

Every designator follows the same shape: an exclamation mark, an event reference (which previous line — e.g. !! for the last command, !ssh for the last command starting with ssh, !:! or ! for the immediately previous line by default), then optionally a word designator (which part of that line — !$ for its last argument, !:2 for its second word, !* for all arguments), and optionally a modifier that transforms the extracted text. Short forms exist: !$ = !:$, !* = !:*.

Two complementary tools

  1. !! / !$ / !:2 / !ssh

    fast, inline, shell-specific (not in plain POSIX sh)

  2. fc -e $EDITOR

    POSIX-standard, opens previous command(s) in your editor for full rewriting

Concrete mechanics worth memorizing

essential

!! reruns the previous command; !ssh reruns the last command beginning with ssh

essential

!$ is the last argument of the previous command; !* is all arguments

power move

Substitution across several past commands to swap prod-01 for test-01 in each, rerunning all three at once

safety

The :p modifier prints the expansion into history without running it — review, then !! or arrow-up+enter to fire it

safety net

shopt -s histverify (bash) or setopt HIST_VERIFY (zsh) makes expansions land in the prompt for editing/discarding before they execute

gotcha

! is easy to trigger by accident (e.g. inside a string) — set +H (bash) or setopt nobanghist (zsh) disables it if unwanted

customization

The trigger character itself can be changed from ! to anything you prefer

Bang syntax vs. the alternatives

Event designators (!)

  • Extract a specific past argument or word directly
  • Faster than search when you already know what you want: !ssh vs ctrl-r+ssh
  • Shell-specific: bash/csh/tcsh/zsh, not vanilla POSIX sh

ctrl-r history search

  • Gives a template you still have to edit
  • Great when you don't remember the exact command

alt-. last-argument recall

  • Good for one narrow job: cycling previous last-arguments
  • Doesn't cover substitution or word-level extraction

fc (POSIX)

  • Opens command(s) in $FCEDIT/$EDITOR (falls back to ed)
  • Best for long or multi-command edits, works on any POSIX shell

Author's stance

The author is explicit this isn't dogma: "I am not aiming for some sort of either-or situation… whatever gets the work done is what you should use," and warns that personal workflow preference outweighs any article. The pitch is modest — save four to forty-two keystrokes, a few hundred times a week, over a career — not a productivity revolution.

Sources