Few tips to use with bash command history.

  1. Erase duplicates in your history file
  2. If you repeat some times the same command, like I usually do when checking memory with
    free -m
    You will end up with a lot of those command repeated through your command history, and if you type

    history

    You may see an output like this:
    ls -l
    ls -h
    free -m
    free -m
    free -m
    free -m
    free -m
    
    If you want to get rid of the reapeated commands, set the variable HISTCONTROL to erasedups.

    export HISTCONTROL=erasedups

    And the output of the command

    history

    will be:
     990 ls -l
     991 ls -h
     992 free -m
     993 history
    
  3. Ignore Duplicates on bash command history, if they are in a row
  4. If you set HISTCONTROL variable to ignoredups, it will not store repetitive commands in a row, but it will store dups if you do something like this.
    ls
    df
    ls
    ls
    ls
    df
    df
    
    You will see the output of

    history

    Like this:
     1000 ls
     1001 df
     1002 ls
     1003 df
     1004 history
    
  5. Control which commands are stored and which are not
  6. If you want full control over which commands will be stored by history and which will not be stored, set HISTCONTROL to ignorespace

    export HISTCONTROL=ignorespace

    This will cause that if a command starts with a space, it will not be stored in your bash history, so you can run
    ps auxw
     df -h
    ps auxw
    
    And the output of

    history

    Will be:
     1015  ps auxw
     1016  ps auxw
     1017  history
    
  7. Ignore dups and ignore space at the same time
  8. You may set the bash variable HISTCONTROL to ignoreboth, and it will act as with ignoredups and ignorespace.

The first one is the one I prefer, how about you?

I have already written about this here but I think now is better explained.