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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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?
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0