Objective: Disable command line history logging in bash shell.
Bash has a few environment variables to control how commands are saved to the history list. They are:
- HISTCONTROL: If the value includes ‘
ignorespace
‘ or ‘ignoreboth
‘, then lines which begin with a space character are not saved to the history file. - HISTFILE: Points to the location of the history file. By default it is
~/.bash_history
. If this is unset, command history is not saved.
So, to disable selective commands from being saved to the history file list, initialise HISTCONTROL
variable to ignorespace
or ignoreboth
in the .bashrc
file in your home directory.
1 2 3 |
# don't put duplicate lines or lines starting with space in the history. # See bash(1) for more options HISTCONTROL=ignoreboth |
Once done, logout and login to the shell for the changes to take effect. Then to prevent sensitive commands from being saved to the command history file, use the following syntax – add a space before entering the command.
1 |
$ <space>sensitve-command |
To prevent the shell from writing all commands executed within an interactive shell to the history file, just unset the HISTFILE
environment variable.
1 |
$ unset HISTFILE |
In this way, the interactive shell will not update the history file when it exits.