Can I search history in bash and run the result?
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 (
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 has many facilities to search and access interactive command history. The most basic of which is the
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
Will execute the
You can also specify relative negative offsets when using the
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
and press Enter. And what about
The most important point from hayalci's response is the call to the
$ shopt -s histverify
This will enable history verification so that commands that are matched by the
As an alternative to crtl+R, you can search history by typing
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.
This instructs bash such that, after any history actions (like
You could also do:
it would return something like
then you can type
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.
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:
At the bash command prompt, type control-R, then type a few characters of the command you want and bash's readline facility will search through the command history for that command.
After you have started the search, you can type control-R again to jump to the next matching command.
I have a really great alias, h. It's really just "history | grep", but I filter out previous "h command" entries with the "grep -E -v"
CTRL+R works just fine, like @John1024 suggested, but is somewhat a bit tedious if many of your recent commands are similar and you want a quick way to scan them all. An alternative would be to use
As navigation through history using Ctrl-r is IMO cumbersome, you may want to consider hh:
which makes navigation much simpler, straightforward and efficient - including running the command: