Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Can I search history in bash and run the result?

share|improve this question
add comment

5 Answers 5

up vote 99 down vote accepted

Type Ctrl R at the command line and start typing the previous command. Once a result appears keep hitting Ctrl R to see other matches. When the command you want appears, simply press Enter

Note that while Ctrl R is the default, if you wanted the command (reverse-search-history) to be bound to Ctrl T you could configure that with the following:

bind '"\C-t": reverse-search-history'

There are a whole host of other readline bindable commands that are available to you as well. Take a look at the bash man page.

Bash has many facilities to search and access interactive command history. The most basic of which is the history builtin. Typing just:

$ history

Will print a list of commands along with a numeric index, like:

$ history
1 clear
2 ls -al
3 vim ~/somefile.txt
4 history
$

You can then execute any of these commands using their numeric index by prefacing the index with a single !, as Mitch pointed out:

$ !1

Will execute the clear command. The history builtin has many features itself, you can see more in the bash and history man pages.

You can also specify relative negative offsets when using the ! designator, so using our history list above, if we wanted to execute vim again, we could do:

$ !-2

Which is basically telling bash to execute the command you ran "two commands ago." To run the previous command in the history list, we can just use !! (which is just shorthand for !-1).

The ! designator doesn't limit you to numerically specifying which command to run . hayalci showed that you can instruct bash to execute a command based on either the text it begins with (using !) or text within the command itself (using !?). Again, using our example history list above, if we wanted to execute clear again, all we need to do is type:

$ !cl

and press Enter. And what about vim? That is as simple as:

$ !?some

The most important point from hayalci's response is the call to the shopt builtin:

$ shopt -s histverify

This will enable history verification so that commands that are matched by the !, !!, and !? designators are not blindly executed, but instead filled in on the command line so you can ensure they will do no evil before executing them. This is even more important when you are executing commands as the root user. This option can be set in your .bashrc startup file so that it is set whenever you log in.

As has already been pointed out, all of this information can be gleaned from the bash man page. For the !, !!, and !? designators, take a look at Section 9.3 History Expansion.

share|improve this answer
11  
+1 I have been Using Linux for over 12 years for all systems. I didn't know that existed. Wish I could +10 how did I miss it?? –  Aiden Bell Jul 17 '09 at 19:27
    
I didn't discover it until I started using emacs regularly. Then I accidentally typed it in bash one day (thinking I was in emacs). –  Sean Bright Jul 17 '09 at 19:28
    
On many systems, pressing the up-arrow does the same. –  mas Jul 17 '09 at 20:29
2  
Up-arrow for me just goes to the previous command. But yeah, it is all bindable to whatever you want. –  Sean Bright Jul 17 '09 at 20:33
    
If somebody were to consolidate '!', '!!', '!?', 'history | grep', and 'ctrl-r' into a comprehensive answer, I would select it –  Richard Hoskins Jul 19 '09 at 14:09
show 2 more comments

As an alternative to crtl+R, you can search history by typing

!text

This will search the history for the most recent command beginning with 'text'.

But I suggest you put this in your .bashrc to prevent execution of wrong command.

shopt -s histverify

This instructs bash such that, after any history actions (like !!:s/prev_text/after_text), it places the resulting line to the command prompt. Then you can review or edit the command, and press Enter afterwards.

share|improve this answer
3  
Use !?text to re-execute the most recent command containing (as opposed to beginning with) "text". –  mark4o Jul 18 '09 at 20:36
    
If somebody were to consolidate '!', '!!', '!?', 'history | grep', and 'ctrl-r' into a comprehensive answer, I would select it. –  Richard Hoskins Jul 19 '09 at 1:21
add comment

You could also do:

history | grep "stuff"

it would return something like

num stuff

then you can type

!num
share|improve this answer
add comment

Excellent writeup, Sean! I'd put this in a comment, but I'm a few reputation points shy. :-)

Another related and useful technique is the ability to run a previous command while changing a word. Say you typoed the directory name, or want to change the file name:

$ echo my name is bob
my name is bob
$ ^bob^jordan
echo my name is jordan
my name is jordan

Notice that the command is expanded, replaced, and output before the command is run, so if the wrong command is run you can see what bash thought it was doing.

share|improve this answer
add comment

I prefer to use history-search-backward over reverse-search-history. The former lets you type a few characters of the command then press the search key, as opposed to hitting the search key first then typing the search string.

By default on my system, M-p and M-n bind to similar functions but I prefer binding the arrow keys:

bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
share|improve this answer
add comment

Your Answer

 
discard

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.