Command line
Bash Notes
Small things about the Bash shell that save time or prevent accidents, written down as they come up. The first topic is the ! history shortcuts and the histverify setting that makes them safe. Commands were checked on Bash 3.2, the version that ships with macOS, and work the same on newer versions.
History expansion with !
Bash remembers every command you run. Type history to see the list with a number beside each one. A ! at the prompt pulls a command, or part of one, back out of that list. Bash replaces the shortcut with the real text before running the line.
| You type | Bash runs |
|---|---|
!! | The previous command |
sudo !! | The previous command, with sudo in front |
!515 | Command number 515 from history |
!-2 | The command before the previous one |
!git | The most recent command that started with git |
!$ | The last word of the previous command |
!* | All the arguments of the previous command |
^old^new | The previous command with the first old replaced by new |
The usual use of !$ is to reuse a path:
And ^old^new fixes a typo without retyping the line:
Space matters: !git is history expansion. ! git status, with a space, is something else: it runs the command normally and only flips its success or failure code.
Why it is risky
By default Bash runs the expanded line the moment you press Enter. You never see what the shortcut turned into until it has already run.
A harmless example. After ls -alrth, the last word is -alrth, so !$ alone tries to run that as a command:
The dangerous version is the same mistake with a different history. !rm or !git runs the most recent matching command, which may be the rm -rf build or git push --force from yesterday, in whatever folder you are in now.
The problem in one line: you are running a command you have not read.
histverify: look before it runs
histverify is a Bash option. When it is on, pressing Enter after a history shortcut does not run anything. Bash puts the expanded command back on the prompt so you can read it, edit it, and press Enter a second time to run it.
To check the current setting, or turn it off again:
Cost: one extra Enter. Benefit: no history shortcut can run a command you did not see first.
Make it permanent
shopt -s histverify lasts only for the current terminal window. To have it in every new shell, add the line to your Bash startup file.
On macOS, Terminal opens login shells, which read ~/.bash_profile instead of ~/.bashrc. Either put the line in ~/.bash_profile, or make that file load the other one:
Preview without changing settings
Two ways to see an expansion on a machine where histverify is off.
Print only, with :p
Add :p to any history shortcut. Bash prints the command and does not run it. The printed command becomes the latest history entry, so !! then runs it.
Expand as you type, with magic-space
With this binding, pressing Space after a history shortcut expands it in place, before you press Enter.
Keep history tidy
Repeating a command adds it to history every time, so history fills up with runs of the same line and !-2 stops meaning what you expect.
HISTCONTROL tells Bash what to leave out.
Tip: with ignorespace active, start a command with a space when it contains something you do not want saved, such as a password or a token.
The same thing in zsh
macOS uses zsh as the default shell since Catalina. The ! shortcuts work the same way, but shopt does not exist there. The matching option is HIST_VERIFY.
Not sure which shell you are in? Ask it: