54

Is it possible to open an incognito session in bash?

For example, when we need to enter passwords in commands and don't want bash to add them to history.

2
  • 10
    cat | bash will run a non-interactive bash without prompt, command-line editing (just the line discipline's internal editor) or history. Oct 2, 2014 at 20:47
  • 2
    Write a space in front of your command: ' secretcommand'. Now secrectcommand will not be in bash history. Oct 9, 2014 at 20:34

3 Answers 3

87

When you want bash to stop logging your commands, just unset the HISTFILE variable:

HISTFILE=

All further commands should then no longer be logged to .bash_history.

On the other hand, if you are actually supplying passwords as arguments to commands, you're already doing something wrong. .bash_history is not world-readable and therefore not the biggest threat in this situation:

ps and /proc are the big problem. All users on the system can see the commands you're currently running with all of their arguments. Passing passwords as command line arguments is therefore inherently insecure. Use environment variables or config files (that you have chmodded 600) to securely supply passwords.

6
  • 5
    I've been using Linux 10+ years and never thought to do this +1
    – eyoung100
    Oct 2, 2014 at 20:21
  • 4
    unset HISTFILE has a similar effect. I have a function for this: n() { HISTFILE=; PS1="~${PS1#\~}"; }. That way I can clearly see whether I am in "incognito mode" or not.
    – Lekensteyn
    Oct 3, 2014 at 17:05
  • 14
    +1 for addressing the real problem rather than merely answering the question. An additional option for passing around credentials are Unix domain sockets. Oct 4, 2014 at 21:15
  • 2
    Another useful tip would be to put a space before i.e. " HISTFILE=". any command with space before does not gets logged.
    – FUD
    Nov 14, 2017 at 8:22
  • Another cool thing about this is that even previous commands on that window are also ignores. It is like turning existing session to incognito :p Mar 23, 2018 at 7:49
40
HISTCONTROL=ignorespace

If this option is not already set for bash, it may be just what you need. It is less debilitating than disabling all history. With that set, any commandline starting with a space character will not be saved to the history list.

From these related links:

Why does bash have a HISTCONTROL=ignorespace option?

Why is bash not storing commands that start with spaces?

1
  • 6
    Note this is often the default.
    – Ángel
    Oct 3, 2014 at 8:49
4

You can temporarily disable history: set +o history

set +o history
...
set -o history

There is a difference between disabling history and unsetting HISTFILE:

HISTFILE=
date
ls
HISTFILE=~/.bash_history
history

outputs something like this:

84  HISTFILE=
85  date
87  ls
88  HISTFILE=~/.bash_history
89  history

i.e. all commands are saved in the history list. Type exit to save it.
But

set +o history
date
ls
set -o history
history

outputs something like this:

115  set +o history
116  history

Summary:
set +o history for long sessions.
HISTCONTROL and <space>command at other time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .