Control your BASH history with HISTCONTROL

The bash shell can store your command history. The HISTCONTROL variable controls how bash stores your command history. With that variable you can tell bash not to remember duplicate commands and/or not to remember some commands at all!

HISTCONTROL can have two possible values: ignoredups and ignorespace. ignoredups tells bash to ignore duplicate commands. Setting the ignoredups flag will prevent any duplicate command to be stored. The other flag, ignorespace, tells bash not to store on the history any command starting with a space, you can execute commands and no one will ever know! It would be even cooler if you could ignore duplicate commands and also prevent commands from being recorded… well, just set HISTCONTROL to ignoreboth.

To set the HISTCONTROL variable open your terminal and type the following:

$ export HISTCONTROL=ignoreboth

Now execute some commands:

$ ls
$ pwd
$  echo "hide this one"
$ history | tail -3

You can see that there’s one blank space before the echo, that means that command will not be recorded. The output of the last command will be something like:

   29  ls
   30  pwd
   31  history | tail -3

As you can see the HISTCONTROL variable is very useful to prevent duplicate entries to be stored in your bash history and for executing commands that you don’t want to be recorded at all.