I use Terminal a lot, and sometimes I am running commands, which aren't things I don't want others to see, but more commands that if I accidentally arrowed up to and accidentally executed would cause a lot of trouble.

So I am wondering if there is, or I can make, some sort of Terminal 'incognito mode' which would allow me to, upon the execution of a certain command, stop recording my history, and then only start recording after I either execute a start recording history again command and exit 'incognito mode', or I simply restart the Terminal?

Because I find myself later on going and removing stuff from my .bash_history, when it would be much easier if I could have stopped it recording there in the first place, or at least got it to try to record it somewhere where it just wouldn't be allowed to, and would just end up not recording it.


Solution 1:

Run a command without putting it in history:

Simply put a space before the command. Bash will ignore commands with a prepended space:

Example: Spaceecho "Some secret text"

Note: This only works if the HISTCONTROL variable is set to ignorespace or ignoreboth.


Disable history temporarily:

  • Run Spaceset +o history or Spaceshopt -uo history to disable history.
  • Run set -o history or shopt -so history to enable it again.

Disable history for the current session (won't remember any commands from the session):

unset HISTFILE

Note: You'll be able to see the commands pressing Up until you close the terminal.


Remove a command from the history:

Run Spacehistory | grep "part of your secret command"

It will show a list of previously ran commands, in this format:

user@host:~$  history | grep pkill
  302  pkill $$
  467  pkill gone-cal
  468  pkill actionaz
  500  pkill chrome
  550  pkill super

Select the entry number at the left of the command. You can copy it with Ctrl+Shift+C

Run Spacehistory -d <number> where <number> is the entry number to remove the entry.
You can paste that number with Ctrl+Shift+V


Other interesting answers:

  • @echristopherson
  • @MoithilBiswas
  • @dolt

Solution 2:

You can simply delete the history of one particular terminal session by adding command history -cw after working.

Do not close the terminal before giving this command.

Solution 3:

shopt -uo history should do it best.

Nuking the HISTFILE (et al) variables won't stop your Up history being logged, it just won't push it to disk. This may or may not be a positive thing for you, but given you mention it, I guess you want something better. Changing the shopt history setting stops the whole history mechanism from triggering.

You can turn logging back on with shopt -so history (the -s and -u are set and unset respectively).

Note that the command itself will probably be logged so prepend it with a space to stop it being added to the history before you clear the variable.