The Definitive Guide to Bash Command Line History

Last week I published a tricks to reverse search (and execute) commandas in Bash.
Now, I found a nice guide that you should read:
"The definitive guide to Bash command line history" from Peteris Krumins’ blog.

Play it again, Sam

The next is an extract from the book “BSD Unix Toolbox” by Christopher Negus and Francois Caen[1], and it’s about to working with commands you’ve typed.

To list the entire history, type

history

To list a previous number of history commands, follow history with a number. This lists the previous five commands in your history:

$ history 5
975 mkdir extras
976 mv *doc extras/
977 ls -CF
978 vi house.txt
979 history

To move among the commands in your history, use the up arrow and down arrow. When a command is displayed, you can use the keyboard to edit the current command like any other command: left arrow, right arrow, Delete, Backspace, and so on. Here are some other ways to recall and run commands from your bash history:

Run the previous command

$ !!

Run command number 997 from history

$ !997 ls -CF

Append *doc to command 997 from history

$ !997 *doc ls -CF *doc

Run previous command line containing the CF string

$ !?CF? ls -CF *doc

Run the previous ls command

$ !ls ls -CF *doc

Run previous ls command, replacing CF with l

$ !ls:s/CF/l ls -l *doc

Another way to edit the command history is using the fc command. With fc, you open the chosen command from history using the vi editor. The edited command runs when you exit the editor. Change to a different editor by setting the FCEDIT variable (for example, FCEDIT=gedit) or on the fc command line. For example:

Edit command number 978, then run it

$ fc 978

Edit the previous command, then run it

$ fc

Use nano to edit command 989

$ fc -e /usr/local/bin/nano 989

Use Ctrl+r to search for a string in history. For example, typing Ctrl+r followed by the string ss resulted in the following:

(reverse-i-search)`ss’: sudo /usr/bin/less /var/log/messages

Press Ctrl+r repeatedly to search backward through your history list for other occurrences of the ss string.

____________________________________
1. Wiley Publishing, Inc., 2008, ISBN: 978-0-470-37603-4. Buy it in Amazon or download it from Rapidshare.