Tips and Tricks: Easy and safe bash history searches

摘自: www.redhatmagazine.com  被阅读次数: 156


yangyi 于 2008-02-08 21:32:08 提供


by an editor

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

The information provided in this article is for your information only. The origin of this information may be internal or external to Red Hat. While Red Hat attempts to verify the validity of this information before it is posted, Red Hat makes no express or implied claims to its validity.

原文链接: http://www.redhatmagazine.com/2008/01/03/tips-and-tricks-easy-and-safe-bash-history-searches/