6 Answers
6

Using that, you'll get the expansion of the history command, and can edit it before executing. From the manual:

If the histverify shell option is enabled, and Readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the Readline editing buffer for further modification.

You can put this option into Bash's configuration files (e.g. ~/.bash_profile or ~/.bashrc depending on which you use) to have it enabled automatically.

In Bash, start typing the first few letters of the command and then hit Ctrl+R to start a history search. You can hit Ctrl+R to continue searching backwards in your Bash history. When you find the command you'd like, use the arrow keys to select/navigate around the command to edit and execute to your needs.

You can also hit Ctrl+R with an empty command buffer and start your search from there.

In addition to the history search via ctrl+r already mentioned, you can use the history entry number method you are already familiar with:

!<history entry number>

in the middle of other commands like

echo !100

and even reference arguments from previous commands. For instance suppose in my history, entry 100 was:

ping google.com

I could do a host lookup on google by pulling the first argument from that history entry like so:

host !100:1

This works for the special reference to the previous command

!!

as well. Of course for the numbered entries, you have to know their entry number by either typing history and looking, or piping to grep. If you want to pull from your history by matching the text, you can use the text from a previous command in the ! statement. For example using the fact that I have that ping google.com statement in my history, I can reference it like:

host !ping:1

The thing to keep in mind with the search by text method is that it searches backwards and pulls the first match.

Maybe I’m missing the point of the question, as I doubt that I’m the only person who knows about :p. It stands for “print”. If you type !100:p, the shell will print (i.e., display) command #100 without executing it.

If you don’t know about the : operators in history, you should learn.
As “numeric illustration” points out, :n retrieves the nth word of a command.
You can also use :n-m to get a range; and there are interesting defaults.
For example, :- (leaving n and m blank) gets you all the words except for the last one. :$ gets you the last word. :h is “head”, so if you type

ls -ld /asdfg/qwerty/foo/bar

then

!:- !$:h

turns into

ls –ld /asdfg/qwerty/foo

Oh, yeah, !$ is short for !:$. :s is substitute, as in :s/old/new/.
This is the same as ^old^new^ except for things like

:s can be used on any command (as in !100:s/...); ^^ works only on the most recent command.

:s can be used on individual words, or ranges of words. For example,

!:- !$:s/d/D/

gets

ls -ld /asDfg/qwerty/foo/bar

(rather than ls -lD ...).

:s can be applied globally. The syntax is non-intuitive, but, where :s/old/new/ replaces the first occurrence, :gs/old/new/ replaces every occurrence.