History Expansion
noun
say it: “HIS-tuh-ree ek-SPAN-shun” /ˈhɪstəri ɪkˈspænʃən/
The shell replacing a short code such as two exclamation marks with words from an earlier command before running the line.
What is history expansion?
History expansion lets you reuse what you typed before with a short code that starts with ! (read "bang"). !! is the whole last command and !$ its last word; the shell swaps the code for those words, prints the result, then runs it. sudo !! runs the last command again with sudo. It works when you type at the prompt, not inside scripts.
mkdir -p exports/print/large
cd !$ # cd exports/print/large
In a sentence
- “Thanks to history expansion, sudo !! ran the last command again with sudo in front.”
- “cd !$ used history expansion to reuse the folder Sam had just made.”
Level: Journeyman · More Unix terms at this level
Updated