243

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.

11 Answers11

285

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:

0x2b3bfa0
  • 9,110
  • 7
  • 38
  • 55
45

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.

32

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.

Oli
  • 299,380
20

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.

muru
  • 207,228
dolt
  • 375
15

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
Bruni
  • 11,099
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
Webtm
  • 198
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.

keag
  • 151
3

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.

2

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

vusan
  • 477
1

Another solution would be to run one shell like bash for normal mode that stores history and run different shell like zsh for incognito mode that doesn't store history.
For example when using zsh for incognito mode then you can configure ~/.zshrc like this way below. So that every time a new instance of zsh is invoked then it immediately sources this content of .zshrc and turns off storing the history of commands.

# Zsh shell in incognito mode. Don't keep any history of invoked
# commands and dump them all to /dev/null instead of ~/.zsh_history
HISTSIZE=0
SAVEHIST=0
HISTFILE=/dev/null 

Then you can create desktop icon shortcut named Incognito_console.desktop like this way below

[Desktop Entry]
Name=Incognito console
Exec=x-terminal-emulator -e 'zsh'
Icon=terminal
Type=Application

Then set this incognito console desktop shortcut as executable

gio set ~/Desktop/Incognito_console.desktop metadata::trusted true
chmod +x ~/Desktop/Incognito_console.desktop