How to run commands in bash without showing in history?

There is sometimes a situation when you want to run some command in terminal but you don’t want to see that command in bash history – and that commands usually contains passwords or some confidential information – for which you would not want to be seen if someone logs on to a server with the same login data.

Of course – it’s not advisable to have multiple users who’s know root login data or have ssh key – but that’s sometimes cannot be avoided – for example, when more users work on maintaining the same server but, because of some reason don’t have sudoers group.

History is very important thing in Linux because you have an overview of what has been done before in terminal – sometimes you may need to replay commands from history what is OK, but sometime you need to delete something from it (well – if you screw-up something), forbid the preservation of history, and similar ‘nasty’ things.

In short lines – the variables which begin with HIST are related to history in Linux (HISTSIZE, HISTFILE, HISTCMD, HISTCONTROL, HISTTIMEFORMAT, HISTIGNORE) and if you want to change something on ‘history level’ – you need to change that variables. In our situation – when we want to keep ourself’s from history logging of our commands – we will play with HISTFILE variable. HISTFILE variable is related to file location where your history is saved so if you run: echo $ HISTFILE, you will see where the history file is located. So – if you want to hide your commands while you are logged in – but on session level (when you reconnect – history will be ON) run this:

# or
# or

If you want to delete something from your current bash history, you can do it with combination of unset and your favorite editor, for example:

vim .bash_history

If you want to keep out your commands out of history always – just put unset in your .bash_profile file.

echo "unset HISTFILE" >> ~/.bash_profile ; . ~/.bash_profile

Of course – delete this unset line from your bash_profile if you want to return logging to history.