Home
login

Tame your bash history

I am a packrat, but I do like a bit of order. This makes maintaining my bash history difficult. There are some commands that I use frequently that seem to fill up my history file making it hard to keep some of the lesser used, yet very important commands in the history. Finally sick of the problem, I poured over the manpage for bash and found the section on HISTCONTROL. From the description there, I found that this along with HISTIGNORE, I can almost eliminate my problem of my bash history getting too full of stupid common commands.

I added this to my ~/.bash_profile:

export HISTIGNORE="&:ls:[bf]g:disown:cd:cd[ ]-:exit:^[ \t]*"
export HISTCONTROL=ignoredups:ignorespace:erasedups
export HISTFILESIZE=2000

Here is the snippet from the bash manual that corresponds to these controls:

       HISTCONTROL
              A colon-separated list of values controlling how commands are saved on the  history
              list.   If  the list of values includes ignorespace, lines which begin with a space
              character are not saved in the history list.  A value of  ignoredups  causes  lines
              matching  the  previous  history  entry  to not be saved.  A value of ignoreboth is
              shorthand for ignorespace and ignoredups.  A value of erasedups causes all previous
              lines  matching  the  current  line to be removed from the history list before that
              line is saved.  Any value not in the above list  is  ignored.   If  HISTCONTROL  is
              unset,  or  does  not include a valid value, all lines read by the shell parser are
              saved on the history list, subject to the value of HISTIGNORE.  The second and sub-
              sequent lines of a multi-line compound command are not tested, and are added to the
              history regardless of the value of HISTCONTROL.
       HISTFILESIZE
              The maximum number of lines contained in the history file.  When this  variable  is
              assigned a value, the history file is truncated, if necessary, by removing the old-
              est entries, to contain no more than that number of lines.  The  default  value  is
              500.   The  history  file  is  also truncated to this size after writing it when an
              interactive shell exits.
       HISTIGNORE
              A colon-separated list of patterns used to decide which  command  lines  should  be
              saved  on  the history list.  Each pattern is anchored at the beginning of the line
              and must match the complete line (no implicit `*' is appended).   Each  pattern  is
              tested  against the line after the checks specified by HISTCONTROL are applied.  In
              addition to the normal shell pattern matching characters, `&' matches the  previous
              history  line.   `&'  may  be  escaped  using a backslash; the backslash is removed
              before attempting a match.  The second and subsequent lines of  a  multi-line  com-
              pound  command are not tested, and are added to the history regardless of the value
              of HISTIGNORE.
 

Cool stuff!

Hey, Vernon.. this is cool stuff. I never even know that bash history could have been configured this way! I just copy and pasted directly into my .bashrc! Thanks for writing this! Fabio

Manpage

Do you have some kind of manly distribution of Linux that includes "manpages," because I would like one of those.

Applications supply manpages

Most applications should have manpages with them... I am currently using Ubuntu 10.10. `dpkg -L bash | grep man` shows this:

/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/clear_console.1.gz
/usr/share/man/man1/rbash.1.gz
/usr/share/man/man1/bash.1.gz
/usr/share/man/man1/bashbug.1.gz
/usr/share/man/man7
/usr/share/man/man7/bash-builtins.7.gz
/usr/share/man/man1/sh.1.gz
diverted by dash to: /usr/share/man/man1/sh.distrib.1.gz

The `man` command itself is in the man-db package (run `dpkg -S $(which man)`).

Some other manpages (for libraries and stuff) are in the manpages-dev package.

When all else fails, I do a Google search for 'man whatever'.