Pruning your Bash history for a better Ctrl+R experience

If you are accustomed (like me) to fire Ctrl+R before running any new command, here is a tip to keep your Shell history clean and more search-friendly.

The best way to do this is to prevent some commands (e.g. those with passwords inlined) to be logged, but also to skip saving duplicated commands.

This is all controlled by the environment variable HISTCONTROL. It can take 3 values:

  • ignorespace: lines which begin with a space character are not saved
  • ignoredups: lines matching the previous history entry are not saved
  • ignoreboth: is shorthand for ignorespace and ignoredups

I recommend using ignoreboth. One set, just start your commands with a space will to skip the shell history log.