! invokes history expansion, a feature that originally appeared in the C shell, back in the days before you could count on terminals to have arrow keys. It’s especially useful if you add the current command number to the prompt (
PS1="!$ ") so you can quickly look at your screen to get numbers for past commands.
Now that you can use arrow keys and things like Ctrl-R to search the command history, I don’t see much use for the feature.
One variant of it you might still find useful is
!!, which re-executes the previous command. On its own, I don’t find !!Enter any faster than just ↑ Enter, but it can be helpful when combined into a larger command.
Example: A common pilot error on
sudo based systems is to forget the
sudo prefix on a command that requires extra privileges. A novice retypes the whole command. The diligent student edits the command from the shell’s command history. The enlightened one types
! in this way is enabled in Bash by default in interactive shells and can be disabled with
set +o histexpand or
set +H. You can disable it in Zsh with
If there isn’t a longer answer here there’s certainly one on Super User, since I’ve read one recently. In the bash man page you can find a huge section titled HISTORY EXPANSION on the matter.
You can do a whole host more than just run the last command, or command number X. You can do things like
!cat to run the last command that started with
!?bash?:s/bash/csh/ runs the last command containing
bash but replaces it with
A lot more can be done with
! such as:
- execute a command which is typed before 3 commands:
- execute a command that starts with
and a lot more. See 15 Linux Bash History Expansion Examples You Should Know