Tips and Tricks: Easy and safe bash history searches
by the editorial team
Contributed by Zak Brown
Often the command you need is in your history, how do you find it?
One simple method is to run the history command and pipe it through grep.
$ history | grep cat 110 cat /tmp/foo
You can then run the command by typing ! and the history line number:
$ !110 cat /tmp/foo
Another method is to use the built in history search feature. In this case you
type !? and then the search term. This searches back into the history
for the last instance of the search term and executes it:
$ !?cat cat /tmp/foo
However, this might be dangerous, what if the last instance of your search term
did something unexpected that would damage the system?
$ !?foo cat /tmp/foo > /dev/hda
Ouch! A better solution is to search for the term and append it to your history
without executing. This time we close the search with ?:p
$ !?foo?:p
This will echo the command and place it as the last item in your history without executing. If it is in fact the command you wish to run, just hit the UP ARROW and ENTER







January 3rd, 2008 at 6:00 pm
What about good old Ctrl-R?
January 3rd, 2008 at 6:00 pm
I also find the following two shortcuts usefull:
!! - will execute the last executed command:
cat /etc/host
!!s - will run cat /etc/hosts
!$ - will place the last argument on the last executed command in the current position.
cat /etc/hosts
cat /dev/null > !$ - will run cat /dev/null > /etc/hosts
January 3rd, 2008 at 6:04 pm
I much prefer to use reverse-i-searches. Hit ^r, type in a string and it will automatically complete it using the latest matching line in your history. Hitting ^r again will search backwards to the next matching line.
January 3rd, 2008 at 6:05 pm
hey…. I haven’t used these history features since the early nineties.
CTRL-R (or shift-CTRL-R to go backwards) is your greatest friend.
oh, and thanks RedHat for the awesome software and efforts you guys and gals have made… here’s to 2008, keep it up.
January 3rd, 2008 at 6:06 pm
Whoops, I guess Corba beat me to it.
January 3rd, 2008 at 6:24 pm
he he… me too
January 3rd, 2008 at 10:53 pm
Credit card has revolutionized the way shopping was done. This is basically money in plastic form. Importance of this card depends upon its use. Youngsters of today use this card as a status symbol where as a family member use them for their convenience. In whatever form you are using this card you cannot ignore the importance of it. And why should you ignore this if some one is giving loan on this basis.To find cash advance loans, payday loans, cash advance payday loans, cash till payday visit http://www.cashadvancepaydayloans.org.uk
January 4th, 2008 at 1:02 am
Or use zsh :)
!text
Will complete the history match starting with text before you have to hit enter.
Otherwise, like others, I find the search mechanism useful, and also an alias:
hg=’history | grep’
because really sometimes you just want to search.
January 4th, 2008 at 9:28 am
I didn’t know about that R thing. That’s cool. Quick question:
If I type in a search string and then continue pressing R to go through the choices, if I accidentally go past the one I want, can I change directions and come back to it without restarting the whole search?
January 4th, 2008 at 9:32 am
I mean CTRL-R. I used angle brackets and I guess it was taken as code.
January 4th, 2008 at 9:53 am
Ctl-s is the incremental forward search, which will allow you to go in the other direction if you pass your command in the Ctl-r search (hit ctl-s twice).
Also, Ctl-s may be grabbed by the terminal, I had to “stty -ixon” to test this.
January 4th, 2008 at 11:04 am
As mentioned in my comment above…
use ctrl-r to search forwards and use shift-ctrl-r to go backwards
It’s habit really… my colleague on the next desk insists on using the arrow keys, but it works for him.
January 9th, 2008 at 7:46 am
One of the best books you can read to improve your sysadmin knowledge is OReillys Bash Guide. There is a treasure trove of bash shortcuts available. After reading I realized the little I actually make use off.
January 15th, 2008 at 7:16 pm
Yep, CTRL-R is a great way to do the same thing. I personally don’t like it because it has a captive interface.
zsh is indeed a great shell with great features. I started using it because of it’s great tab completion features. I’ve since gone back to using bash with the bash-completion package installed.
February 3rd, 2008 at 8:14 am
I love CTRL-R, solves all problems :D
February 12th, 2008 at 11:47 am
I like to use the CTRL-X-E to edit multiple lines…
but dont forget to set your preferred editor:
#export EDITOR=vim