Article

maggot62's picture

Tuning the BASH History

Author Info

2 July 2009 - 12:48pm
Submitted by: maggot62

article
Reads:

1049

Score:
5
5
1
 
Comments:

0

I am making the assumption that you know a little about Linux in as much as you are using the command line interface, BASH. Every command that you enter is stored in the file ~/.bash_history. This then means, unlike some legacy systems of ten used as desktops, your command history stays with you even after a reboot.

With that in mind I would like to step you through tuning what we store in our history.

1. From the command line type:

 echo $HISTSIZE

This by default on SUSE is 1000. Meaning 1000 lines of history are stored.

2. Now try this:

 echo $HISTIGNORE

This by default is not set in SUSE and should return nothing.

3. Now type:

history 

Ok you should see a bucket load of commands that have been typed in at the command line by your account.

4. Ok How do I use this history?

  • If you have, lets say, an ls command in your history, then:
    !ls

    will run the last command in your history that started with ls.

  • If you want to create a directory and then enter that directory:
    mkdir  -p /sales/shoe
    cd !$ 
    

    The macro !$ represents the last argument you used,

  • The keys cntrl+r will allow you to reverse search your history, for example:
    cntrl + r
    user
    

    As you start typing the word user, to it's right you will see matches start showing. If you want to run a previous useradd command wait until you see useradd. ESC will place the command on the command line so you can edit it. ENTER will run the command.

Cool, so lets hope now you are ok running the history commands, but lets control what is stored as history. This will have a slight improvement in memory usage on the machine as history tuning will affect both shell memory as well a the file.

1. To start consider typing:

history  -c

This will clear your current user command history, giving you a clear sheet to start with.

2. Set the variable to control what is placed in history:

export HISTIGNORE="ls:su: su -:exit:mount:du"

This is a string list that is colon separated. We are choosing with this setup to ignore:

  • ls on its own
  • su on its own
  • su -
  • exit
  • mount on its own
  • du on its own

Now that is set, and your history is empty. Type

ls

up arrow key and it will not be there.

ls /etc

Will be in the history as it was only ls on it's own we were excluding. If then, is what you want, and why shouldn't it be. Add this to all users but placing the export line in the file /etc/bash.bashrc.local.

Feel free to re-use the contents of the attached odp file as you wish.

AttachmentSize
bash-history.odp40.62 KB

Author Info

2 July 2009 - 12:48pm
Submitted by: maggot62




User Comments

© 2009 Novell, Inc. All Rights Reserved.