241

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.

7
  • 6
    Well, presumably you are paranoid that your history will be read by others?
    – Sparhawk
    May 19, 2015 at 10:11
  • 6
    @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
    – user364819
    May 19, 2015 at 10:13
  • 19
    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")
    – ereOn
    May 19, 2015 at 13:01
  • 2
    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.) May 23, 2015 at 20:09
  • 1
    See this question on serverfault
    – Luc M
    Sep 15, 2015 at 3:25

10 Answers 10

283

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:

13
  • 8
    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
    May 18, 2015 at 15:38
  • 1
    It just reminded me of the super-injunction logic, that's all ;)
    – AJFaraday
    May 18, 2015 at 16:00
  • 4
    (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.
    – AJFaraday
    May 18, 2015 at 16:06
  • 1
    @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...
    – Tim
    May 18, 2015 at 19:55
  • 4
    The unset HISTFILE approach also works retroactively for all commands which haven't been written to .bash_history yet.
    – kasperd
    May 19, 2015 at 14:23
44

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.

3
  • This will not remove all the history?
    – 0x2b3bfa0
    May 19, 2015 at 16:54
  • 6
    This will only remove history of that particular terminal session. May 19, 2015 at 20:42
  • 1
    Be aware. This command cleared my .zsh_history. Apr 9, 2021 at 0:50
30

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.

19

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.

2
  • 9
    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.
    – Guido
    May 23, 2015 at 18:30
  • 4
    I use zsh with incremental history write, so I doubt this would work in my case Sep 15, 2015 at 12:20
14

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
6
  • 1
    And is there an easy way in the current session to reverse this, and make it carry on?
    – user364819
    May 18, 2015 at 15:04
  • @ParanoidPanda I added a command for reverting in the current session.
    – Bruni
    May 18, 2015 at 15:14
  • 1
    @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.
    – 0x2b3bfa0
    May 18, 2015 at 16:17
  • @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.
    – user364819
    May 18, 2015 at 16:29
  • 1
    @ParanoidPanda, Helio seems to be right on this one as the history is saved on exit. I had not considered that one.
    – Bruni
    May 18, 2015 at 17:47
10

You can modify history lines in the current shell session. If you walk back through history (e.g. with Up or Ctrl+P) and change a line without executing it, only the modified version of the line will be saved. You can modify it in any way you like; good choices would include using Ctrl+U to totally blank the line or using Esc# to put a # comment character at the beginning.

To leave behind a line without executing it, remember not to hit Return (Enter). What I do is return to the newest line in history via Esc>.

(Instead of Esc followed by >, in most terminals you can hold Alt and press >; the same goes for the Esc# suggestion above.)

Note: You can’t permanently modify entries from previous sessions this way. Those changes will not be applied to the HISTFILE at the end of the session.

6

You could also make .bash_history read-only. I would empty the file first and then do:

 chattr +i .bash_history
5

You could execute the sensitive commands in a screen session, then destroy the session when you are done.

Alternately, you could keep that screen session alive but detached, and only access it when you actually want to be able to up arrow to the otherwise dangerous commands.

1

Personally I use mksh, and this shell by default has no history file. That means you could launch alternative shell such as mksh or dash (which comes by default by the way) to run the command you don't want users to see, and exit once done. bash history will only record that you launched alternative shell and that's it.

1

You can also use terminal multiplexer such as tmux, screen. So under tmux or screen history would not be saved.

You must log in to answer this question.