Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I used

history | less

to get the lines of previous commands and from the numbers on the left hand side I found the line I wanted repeated (eg. 22) and did


at the command prompt and it worked -- executing the set of commands on the line I did at that time. I cannot figure out where the exclamation mark is used, what does it represent in terms of actions taken by bash, and where to use it. From the documentation I do not see an explanation that is 'tangible'.

share|improve this question
This answer might help – Michael Mrozek Nov 3 '10 at 21:50
Not an answer to your question, but <ctrl>+R will allow you to interactively search your history and then immediately execute if you find what you were looking for. – kasterma Nov 3 '10 at 22:25
up vote 72 down vote accepted

! is 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 sudo !!.

Bash lets you disable ! processing in the shell with set +o histexpand or set +H. You can disable it in Zsh with set -K.

share|improve this answer
I agree about Up Enter – phunehehe Nov 4 '10 at 14:43
I find Ctrl-P Ctrl-J to be pretty fast; faster than Up Enter, at least. – ephemient Nov 5 '10 at 1:55
If you just want to run the last command, Up/Enter is fine, but if you want to add to it in some way (say, you forgot to sudo, or something), then you can do sudo !!, for example. That might be a bit faster than "Up/Ctrl-A(or Home, if you are lucky enough)/sudo/space/enter". YMMV. :) – malvim Jan 14 '11 at 20:57
omg, thx now I get its meaning! btw, cant it simply be disabled? it has caused me so much trouble... every echo that ends with ! causes trouble.. – Aquarius Power Jul 18 '13 at 15:11
@AquariusPower: Just quote it. Either echo 'Hi!' or echo "Hi!" or echo Hi\!. All do the same thing. – Warren Young Jul 18 '13 at 16:14

If there isn't a longer answer here there's certainly one on Super User, since I've read one recently. The man page has 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 cat. Or !!:s/bash/csh/ runs the last command containing bash but replaces it with csh.

share|improve this answer
Although not directly an answer to OP's question, this needs way more upvotes. So useful! Thanks, Havok. – malvim Apr 30 '15 at 19:09
Uh, I should really look into the man page before I start googling. – erikb85 Jul 27 '15 at 13:39
!!:s/bash/csh/ actually runs the last command, then replaces bash with csh, if present. Not the last command that included bash. That would be !?bash?:s/bash/csh/ or maybe !?bash?:s/%/csh/ – cde Jan 24 at 14:31

A lot more can be done with ! such as:

  • execute a command which is typed before 3 commands: !-3
  • execute a command that starts with !ls

and a lot more. See 15 Linux Bash History Expansion Examples You Should Know

share|improve this answer

A friend of mine emailed me this:

It's part of GNU history library. In bash it is used to re-run commands in your history. If you want to be hardcore, grep for history_expansion_char in bash-4.1/lib/readline/histexpand.c for implementation details.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.