Linux.com

Feature

CLI Magic: Using command history in the bash shell

By Mark Sobell on July 03, 2006 (8:00:00 AM)

Share    Print    Comments   

The Bourne Again Shell's history mechanism, a feature adapted from the C Shell, maintains a list of recently issued command lines, also called events, providing a quick way to reexecute any of the events in the list. This mechanism also enables you to execute variations of previous commands and to reuse arguments from them. You can replicate complicated commands and arguments that you used earlier in this login session or in a previous one and enter a series of commands that differ from one another in minor ways. The history list also serves as a record of what you have done. It can prove helpful when you have made a mistake and are not sure what you did, or when you want to keep a record of a procedure that involved a series of commands.

User Level: Intermediate

This article is excerpted from chapter 9 of the new Third Edition of A Practical Guide to Red Hat Linux: Fedora Core and Red Hat Enterprise Linux , copyright 2007 Pearson Education, Inc. ISBN 0132280272. Published June 2006 by Prentice Hall Professional. Reproduced by permission of Pearson Education, Inc. All rights reserved.

Variables that control history

The value of the HISTSIZE variable determines the number of events preserved in the history list during a session. A value in the range of 100 to 1,000 is normal.

When you exit from the shell, the most recently executed commands are saved in the file given by the HISTFILE variable (the default is ~/.bash_history). The next time you start the shell, this file initializes the history list. The value of the HISTFILESIZE variable determines the number of lines of history saved in HISTFILE (not necessarily the same as HISTSIZE). HISTSIZE holds the number of events remembered during a session, HISTFILESIZE holds the number remembered between sessions, and the file designated by HISTFILE holds the history list.

Variable

Default

Function

HISTSIZE

500 events

Maximum number of events saved during a session

HISTFILE

~/.bash_history

Location of the history file

HISTFILESIZE

500 events

Maximum number of events saved between sessions

The Bourne Again Shell assigns a sequential event number to each command line. You can display this event number as part of the bash prompt by including \! in the PS1 variable. Examples in this article show numbered prompts when they help to illustrate the behavior of a command.

Give the following command manually or place it in ~/.bash_profile (to affect future sessions) to establish a history list of the 100 most recent events:

 $ HISTSIZE=100 

The following command causes bash to save the 100 most recent events across login sessions:

 $ HISTFILESIZE=100 

After you set HISTFILESIZE, you can log out and log in again, and the 100 most recent events from the previous login session will appear in your history list.

Give the command history to display the events in the history list. The list of events is ordered with oldest events at the top of the list. The following history list includes a command to modify the bash prompt so that it displays the history event number. The last event in the history list is the history command that displayed the list.

32 $ history | tail
   23  PS1="\! bash$ "
   24  ls -l
   25  cat temp
   26  rm temp
   27  vim memo
   28  lpr memo
   29  vim memo
   30  lpr memo
   31  rm memo
   32  history | tail

As you run commands and your history list becomes longer, it may run off the top of the screen when you use the history builtin. Pipe the output of history through less to browse through it, or give the command history 10 to look at the 10 most recent commands.

You can reexecute any event in the history list. Not having to reenter long command lines allows you to reexecute events more easily, quickly, and accurately than you could if you had to retype the entire command line. You can recall, modify, and reexecute previously executed events in three ways: You can use the fc builtin (covered next); the exclamation point commands; or the Readline Library, which uses a one-line vi- or emacs-like editor to edit and execute events.

If you are more familiar with vi or emacs and less familiar with the C or TC Shell, use fc or the Readline Library. If you are more familiar with the C or TC Shell and less familiar with vi and emacs, use the exclamation point commands. If it is a toss-up, try the Readline Library; it will benefit you in other areas of Linux more than learning the exclamation point commands will.

Next: Using fc
 

Share    Print    Comments   

Comments

on CLI Magic: Using command history in the bash shell

Note: Comments are owned by the poster. We are not responsible for their content.

Privacy

Posted by: Anonymous Coward on July 03, 2006 05:37 PM
For privacy I often remove the write attribute from the<nobr> <wbr></nobr>.bash_history file to prevent bash from writing my commands down there.

#

Re:Privacy

Posted by: Anonymous Coward on July 04, 2006 02:18 AM

You can also make your<nobr> <wbr></nobr><tt>.bashrc</tt> '<tt>unset HISTFILE</tt>' to prevent bash even from trying to maintain a history file

Good for root's<nobr> <wbr></nobr><tt>.bashrc</tt> methinks...<nobr> <wbr></nobr>:-)

-Tweek

#

interactive way

Posted by: Anonymous Coward on July 03, 2006 06:29 PM
I often use ctrl-r to start an history search (reverse-i-search) : ctrl-r + you type part of a previous command (and you can ctrl-r to search further backward) and you can edit the command before pressing enter.

#

Re:interactive way

Posted by: Anonymous Coward on July 04, 2006 01:33 AM
i just found out about the emacs ^r search, which has been great (just type any portion of the command you're looking for). also, setting HISTCONTROL to ignore duplicates can way-shorten the `history -200 | less` routine

#

Re:interactive way

Posted by: Anonymous Coward on July 04, 2006 03:26 AM
One of the things that prevents my switching to bash is the absence of an equivalent to tcsh's Esc-p keybinding.

Say your history looks like this:


      23 PS1="\! bash$ "

      24 ls -l

      25 cat temp

      26 rm temp

      27 vim memo

      28 lpr memo

      29 vim memo

      30 lpr memo

      31 rm memo

      32 history | tail

In tcsh, if you type "l" (the letter L) followed by Esc-p, history is searched backwards for the first command that started with "l". Keep hitting Esc-p and it keeps going back in history to find the correct one.

So in this case, you would first see "lpr memo", then "ls -l", etc. If you pass the one you want, you can go back by hitting Esc-n.

In many instances, it really beats the pants off bash's Ctrl-r.

Is there the same thing in Bash?

L

#

Re:interactive way

Posted by: Anonymous Coward on July 04, 2006 04:45 PM
Yes, there is something similar, and I'm using it all the time. By changing the setting of bash you can write the start of a command and using the up or down key to cycle through history of command starting with what you typed.

for details, see (that's where I learned about this):
<a href="http://www.geocities.com/h2428/petar/bash_hist.htm" title="geocities.com">http://www.geocities.com/h2428/petar/bash_hist.ht<nobr>m<wbr></nobr> </a geocities.com>

regards,
n.

#

too much keybindings to learn

Posted by: Anonymous Coward on July 03, 2006 08:08 PM
is there any other shell that has most of the nice features of bash but without requiring you to learn all these key bindings and symbols? I mean something with a nice intuitive ncurses interface.

(mc would be nice but it has to do a lot more than that so it doesn't emphasize on the command prompt power)

#

Re:too much keybindings to learn

Posted by: Anonymous Coward on July 06, 2006 07:10 PM
No, I am not aware of any such shell.
But maybe you want to innovate the next-generation super shell?
Go ahead!<nobr> <wbr></nobr>:)

#

Re:too much keybindings to learn

Posted by: Anonymous Coward on July 07, 2006 12:00 AM
hmm... I might have been missunderstood

It's definetly not the power that I see lacking in shells like bash - to the contrary there is plenty of it. What I would like to see are convinience, easy learning curve, easy exploration of the shell capabilities etc.

That is it's not easy for a new user to use even a small part of bash capabilites. They are hiden. It's the typical "read the man page, experiment, read the man again, work for a few days, oops you forgot the shortcut? go read the man page again - after some months you are a guru, go impress your friends". I prefer the other aproach "read the first 2 pages of the manual, play with it, learn as you play, let the UI provide feedback and help you explore it, when you're really up to doing something impressive read the other manual pages and experiment a few times"

An example: I usualy forget the little important details of how to search for an html file that contains the word "linux" through the shell. I can NEVER forget how to do the same in mc - it's damn obvius. Ofcourse I can't make complex searches in mc but I could do 90% of the searches I care to do if mc had just a few more controls in the search window.

#

There's an easier way for those who use vi

Posted by: Anonymous Coward on July 05, 2006 11:28 PM
Just type "set -o vi" at the command prompt.

Now you can then recall the previous command by pressing "Esc k". Press "k" again to get the next previous command. Press "j" and you come back down the list. Press "l" or "h" to go left or right, just like in vi. You can use vi commands to edit the command.

Sure is easier than all this "fc" stuff!

#

Re:ignore duplicate &amp; commands

Posted by: Anonymous Coward on July 06, 2006 07:12 PM
Good idea to ignore some commands, then it only logs important stuff such as which files get edited, which hosts get pinged, nmapped, etc...

#

Re:Red Hat Rocks

Posted by: Anonymous Coward on July 13, 2006 02:02 AM
These aren't features of Red Hat. It's features of bash and available on any system that uses bash.

#

Red Hat Rocks

Posted by: Administrator on July 04, 2006 02:53 PM
It is this cool stuff that makes Red Hat Linux the choice for me.

#

ignore duplicate &amp; commands

Posted by: Administrator on July 04, 2006 02:28 AM
# ignore duplicate

HISTCONTROL=ignoreboth



# ignore these in history file

HISTIGNORE=ls:ll:la:l:cd:pwd:exit:mc:su:df:clear

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya