Useful Bash History Settings
This post aims to describe some useful bash history settings, that are all too often unknown of. Following are some environment variables described, which can be set in the users bashrc file in eg. ~/.bashrc
.
HISTCONTROL
Setting | Behaviour |
---|---|
ignorespace |
Do not record if line starts with a blank/space |
ignoredups |
Do not record line if its a duplicate of the previous line |
ignoreboth |
Do ignorespace and ignoredups |
erasedups |
Do remove all occurences of line before saving line |
The settings can be combined by colons, eg. ignorespace:erasedups
.
Example: HISTCONTROL="ignorespace:ignoredups"
HISTIGNORE
A colon :
separated list of patterns. Patterns may include special *
and &
characters.
*
is a wildcard match, please note that matching a whole line is not implied at the end of your patterns.
You must explicitly set *
. &
matches the previous line in the history.
Escaping can be done via double backslashes, eg \\&
.
Example: HISTIGNORE=cd *:ls *:&
HISTSIZE
The amount of lines to save in the history. This defaults to 500
.
Example: HISTSIZE=1000000
HISTFILE
The path where the history is stored. Defaults to ~/.bash_history
.
Example: HISTFILE=/dev/null
Condensing the History
My favorite history setting is to condense the history instead of using it as a log of consecutive commands.
Save to ~/.bashrc
or system-wide in /etc/bash/bashrc
HISTCONTROL='ignorespace:erasedups'
HISTIGNORE='cd *:ls *'
HISTSIZE='1000000'
Deactivating the History
Some hackers use this trick to not get commands logged.
export HISTFILE=/dev/null
Useful History Commands and Key Combinations
Command or Combination | Description |
---|---|
history -a |
Append current shell instance in-memory history to the history file |
Ctrl-r |
Reverse search the history. Press enter for execution of command |