208

Is there a way to temporarily suspend history tracking in bash, so as to enter a sort of "incognito" mode? I'm entering stuff into my terminal that I don't want recorded, sensitive financial info.

0

7 Answers 7

289

This will prevent bash from saving any new history when exiting the shell:

unset HISTFILE

Specifically, according to man bash:

If HISTFILE is unset, or if the history file is unwritable, the history is not saved.

Note that if you re-set HISTFILE, history will be saved normally. This only affects the moment the shell session ends.

Alternatively, if you want to toggle it off and then back on again during a session, it may be easier to use set:

set +o history # temporarily turn off history

# commands here won't be saved

set -o history # turn it back on
10
  • 29
    The value of HISTFILE is only checked when bash exits, so the first method doesn't work as is (if you restore the value, the command will be saved). set +o history does work as directed. Commented Apr 8, 2011 at 20:10
  • 8
    Thanks, excellent. I'll use set +o history and set -o history to toggle back and forth when I'm doing secret stuff ;) Commented Apr 8, 2011 at 23:38
  • 2
    unset HISTFILE does not work. set -/+o history works like a charm! thanks Commented Oct 28, 2015 at 13:10
  • 11
    As a tip if you put a space before command (start the command with a space) it will not be recorded in your history, and up/down arrow keys will not show it either.
    – Mahdi
    Commented Jul 3, 2017 at 16:21
  • 2
    @Tebe it mentions the use of ‘+’ to disable the option at the very bottom. Granted it’s weird that a plus disables but they really had no choice since the dash-option has historically been used to enable an option.
    – SiegeX
    Commented Jan 18, 2021 at 17:12
51

Using bash, set HISTCONTROL="ignorespace" and precede with space any command you do not wish to be recorded in history. In case you forgot to take any measures, there is also history -d <number> for deleting a specific entry or history -c for clearing the entire command history.

2
  • 1
    Put the HISTCONTROL="ignorespace" into ./~bashrc file, then run the source ~/.bashrc command.
    – Feriman
    Commented Aug 26, 2022 at 13:37
  • For users of zsh setopt HIST_IGNORE_SPACE # Don't record an entry starting with a space.
    – bambam2174
    Commented Feb 3, 2023 at 14:26
31

Make sure that HISTCONTROL contains ignorespace. You'll probably want to add HISTCONTROL=ignorespace (or HISTCONTROL=ignoredups:ignorespace or something) to your ~/.bashrc. Then any command line that begins with a space is omitted from the history.

Another possibility is to start a new bash session that doesn't save its history.

$ bash
$ unset HISTFILE
$ sooper-sekret-command
$ exit
$ #back in the parent shell
9

If you need to avoid storing several commands and you still want to use up arrow to access previous commands, use:

$ bash                        # open a new session.
$ unset HISTFILE              # avoid recording commands to file.
$ commands not recorded
.
.
$ exit
$

There are four ways (levels) to control how commands are stored.

  1. The first and simplest is to use ignorespace (or ignoreboth):

    $ HISTCONTROL="ignorespace${HISTCONTROL:+:$HISTCONTROL}"
    

    That will allow to use an space before the commands that you want to avoid being recorded in the memory list of history. And, in consequence, as there is no command recorded in memory that could be sent to file, will also avoid one command to be sent to the file listed in $HISFILE.

  2. Avoid recording commands to the file in $HISTFILE:

    $ unset HISTFILE
    

    If unset, the command history is not saved when a shell exits.

    Null HISTFILE='' and/or set to HISTFILE=/dev/null works to the same effect. Understand that commands are still being recorded to the memory list , try the history command, or the up arrow.

    Warning: if HISTFILE is reset before the shell exists, all what has been recorded in memory could be written to the file anyway.

  3. Avoid recording new commands to the history list in memory. And, as not being in memory, can not be recorded to file.

    $ shopt -ou history              # or set +o history
    

    Re-enable with shopt -os history (or set -o history)

  4. Remove all commands from to the history list in memory:

    $ HISTSIZE=0
    

    All commands get erased (from memory) and therefore nothing could be stored to file, of course, until the variable is set again to some valid numeric value.

7
  • // , This covers all the answers except the answer from @Ambrose in one convenient page. Way to go, @Isaac. Would you be willing to add a link or two to the docs for this? Pretty sure man history isn't going to cut it for most of us. Commented Oct 15, 2018 at 22:46
  • 1
    @NathanBasanese Expanded. Better?
    – user232326
    Commented Oct 17, 2018 at 15:58
  • // , Yes! Would I be correct in assuming that these come from the Bash Variables and Shopt Built-in docs? Commented Oct 17, 2018 at 16:52
  • // , Also, while I was reading the Bash Variables documentation, I came across HISTIGNORE, which subsumes the function of HISTCONTROL. Commented Oct 17, 2018 at 16:55
  • (Assuming you are using linux): What happens if you execute the command LESS=+'/^ *HISTI' man bash (isn't that the "manual pages"?). @NathanBasanese
    – user232326
    Commented Oct 17, 2018 at 21:38
3

There is one simple way to turn off the history, so commands won't be stored in the .bash_history file.

You have to put the whitespace or tab space in front of any command, so that command won't be stored in the history. For example:

$ ls 
print the list of file 
$ history 
ls
history

$  pwd
print the current  working directory 
$ history 
ls
history

The pwd command will not get store in the history, because it has whitespace in the front.

2
  • This does not work; Bash complains that '-' or '-history', or other hyphenated commands cannot be found. Commented Nov 12, 2015 at 21:52
  • 4
    This wont work unless HISTCONTROL="ignorespace" is set as in forcefsck's response Commented Mar 21, 2016 at 23:37
2
export HISTFILE=/dev/null 

That is my goto way. Just in case the unset HISTORY/HISTFILE/HISTCONTROL, etc... doesn't work, exporting it to /dev/null has always worked for me.

1
  • For temporarily suspending history? This didn't work for me as a regular command. Subsequent commands were logged to history. Are you calling this in .bash_profile or a script? GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
    – Mat Gessel
    Commented Mar 3, 2019 at 22:26
0

To take SiegeX's answer a bit further and really make this into incognito mode...

Disclaimer: title stuff will probably on work on gnome-terminal. it was only tested in Linux Mint 19.3 / Gnome Terminal 3.28.1 because i'm lazy.

Add the following to your ~/.bash_functions (this assumes you are importing it via your .bashrc / .profile etc):

function setGnomeTerminalTitle() {
    local NEW_TITLE="$1";
    PS1="\[\e]0;${NEW_TITLE}\a\]\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\]";
}

Then add the following to your ~/.bash_aliases (again this assumes you are importing it via your .bashrc / .profile etc):

alias title="setGnomeTerminalTitle";
alias hist='history'
alias histoff="setGnomeTerminalTitle 'Incognito Window' && set +o history"
alias histon="setGnomeTerminalTitle \"$USER@$HOSTNAME:\${PWD//\${HOME//\\//\\\\\\/}/\\~}\" && set -o history"

EDIT: to explain the histon title stuff - normally the window title in gnome-terminal is "$USER@$HOSTNAME:${PWD}" except that if you are in your home folder, /home/foo, it gets displayed as ~; at least it is on my computer. this command is using bash string substitutions to escape the slashes in $HOME so we get \/home\/foo and then in turn using that as a regex to replace that pattern in $PWD with a literal ~ ... and then a bunch of escapes are added on top to make it work as an alias in a double-quoted string.

Now when you type histoff you get a title change for incognito mode and your history is stopped for that session in that window (the word histoff is still logged but nothing after). If you then type histon it reverts the title and all of the lines after histoff up to and including the line with histon are not logged but everything after histon is logged.

Example:

## this is still logged
histoff  

## this is NOT logged
echo "my password is: password";
fake_ua='If firefox and curl had a child, would it be named furl?';
curl -A "$fake_ua" https://www.itsnotap0rnsiteipromise.com/curvy-girlz/;

vi ~/Documents/maths/super-secret-squirrel/gift-ideas.txt

## this is still NOT logged
histon

## this is logged again
sudo apt install -y kmag;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .