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.
Run a command without putting it in history:
Simply put a space before the command. Bash will ignore commands with a prepended space:
Example: Space
echo "Some secret text"
Note: This only works if the
HISTCONTROL
variable is set toignorespace
orignoreboth
.Disable history temporarily:
set +o history
or Spaceshopt -uo history
to disable history.set -o history
orshopt -so history
to enable it again.Disable history for the current session (won't remember any commands from the session):
Note: You'll be able to see the commands pressing Up until you close the terminal.
Remove a command from the history:
Run Space
history | grep "part of your secret command"
It will show a list of previously ran commands, in this format:
Select the entry number at the left of the command. You can copy it with Ctrl+Shift+C
Run Space
history -d <number>
where<number>
is the entry number to remove the entry.You can paste that number with Ctrl+Shift+V
Other interesting answers:
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.
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 theshopt
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.
Another way to kill the current shell without logging to the history file is to do:
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.
To temporary disable the command history for the current session, you can temporarily unset the HISTFILE environment variable.
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)
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.You could also make .bash_history read-only. I would empty the file first and then do:
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.
Personally I use
mksh
, and this shell by default has no history file. That means you could launch alternative shell such asmksh
ordash
(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.You can also use terminal multiplexer such as
tmux
,screen
. So undertmux
orscreen
history would not be saved.