Terminal 'incognito mode'?

25,098

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:

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.

Solution 4

Another way to kill the current shell without logging to the history file is to do:

kill -9 $$

This causes bash (and probably other shells too) to send the SIGKILL signal to itself, killing it on the spot and preventing it from writing anything to disk.

Solution 5

To temporary disable the command history for the current session, you can temporarily unset the HISTFILE environment variable.

unset HISTFILE

while the session is active you can access the history as usual, but it won’t be saved to the disk.

To reverse in the same session (all changes will be recorded)

HISTFILE=$HOME/.bash_history
Share:
25,098

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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.

    • Sparhawk
      Sparhawk almost 9 years
      Well, presumably you are paranoid that your history will be read by others?
    • Admin
      Admin almost 9 years
      @Sparhawk: Well, it's partly that, and party I don't want to accidentally execute a possibly deadly maintenance command when I'm just arrowing up in the future! :D
    • ereOn
      ereOn almost 9 years
      Just to add, you don't have to justify yourself: "which aren't things I don't want others to see". That would be a perfectly valid reason to me too. (Especially in this time and age where everybody tries to make you look guilty for having "things to hide")
    • Hitechcomputergeek
      Hitechcomputergeek almost 9 years
      If you open two tabs, do stuff in the first one, do stuff in the second one, then close the second one BEFORE the first one, only the history of the first one seems to be written to .bash_history. (Ubuntu 15.04 ruined Terminal, lucky this is a live USB.)
    • Luc M
      Luc M over 8 years
      See this question on serverfault
    • jwbensley
      jwbensley over 8 years
      It would also be handy for commands or programs which required you to enter your password or some secure credentials onto the CLI which them become stored in you .bash_history
  • Admin
    Admin almost 9 years
    And is there an easy way in the current session to reverse this, and make it carry on?
  • Bruni
    Bruni almost 9 years
    @ParanoidPanda I added a command for reverting in the current session.
  • AJFaraday
    AJFaraday almost 9 years
    If you use history -d to remove a line, this will remove the content of the line, but you can never remove the fact that you ran history -d (unless you first use set +o history before running history -d)
  • AJFaraday
    AJFaraday almost 9 years
    It just reminded me of the super-injunction logic, that's all ;)
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    @AJFaraday: Sorry, I don't understood your comment, English is not my mother tongue. Can you explain me it?
  • AJFaraday
    AJFaraday almost 9 years
    (OT) Injunctions are a legal document which keeps people from publishing something. A super injunction also keeps them from publishing the fact they were denied the ability to publish. Some logical arguments ask if they can publish that they were denied the ability to publish some information. Then it becomes a cyclical argument.
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    @AJFaraday: ;-) Now I understood, thanks. Yes, you had reason. Lol.
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    @ParanoidPanda: Why you need a command to set HISTFILE again on the current session? If do you undo the change (even after running the secret commands) they will be stored into history when you exit.
  • Admin
    Admin almost 9 years
    @Helio: Oh, no, I meant a command which would then start recording again, but not save the commands which I didn't want it to save.
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    Then unset HISTFILE nor HISTFILE=$HOME/.bash_history will work. Check the Oli answer or mine.
  • Bruni
    Bruni almost 9 years
    @ParanoidPanda, Helio seems to be right on this one as the history is saved on exit. I had not considered that one.
  • Tim
    Tim almost 9 years
    @Helio I've made some quite drastic formatting changes to your answer. I personally think it makes it easier to read - and easier for people to understand your meaning (the Space+Command syntax was confusing for me). Feel free to do a partial / full rollback if you disagree.
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    @Tim: I partially agree with your changes... I think that Space, being a non-printable character, is best to represent it with something "solid", and more being on the start of a line... I agree with the sample output of history. I partially agree with the slow copy/paste method. I'll partially revert your changes. Feel free of revert my reversion if do you have another opinion ;-)
  • Tim
    Tim almost 9 years
    @Helio I think it's better for users to know the "proper" way to copy - especially if they accidentally go and select some text. I'm not sure how to do the space, maybe clarify more that the space is there? The [space] looks a little clumsy to me...
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    And how to clarify a blank character?
  • kasperd
    kasperd almost 9 years
    The unset HISTFILE approach also works retroactively for all commands which haven't been written to .bash_history yet.
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    This will not remove all the history?
  • Moithil Biswas
    Moithil Biswas almost 9 years
    This will only remove history of that particular terminal session.
  • A. Donda
    A. Donda almost 9 years
    "remind" should be "remember"
  • 0x2b3bfa0
    0x2b3bfa0 almost 9 years
    @A.Donda, sorry, English is not my mother tongue.
  • Guido
    Guido almost 9 years
    a.k.a. 'Shoot yourself in the head' :). This has the advantage that you don't need to remember to do anything before writing the commands.
  • Wayne Werner
    Wayne Werner over 8 years
    I use zsh with incremental history write, so I doubt this would work in my case
  • Chris Jeong
    Chris Jeong about 3 years
    Be aware. This command cleared my .zsh_history.