Temporary Disabling Bash History

Say that you’ve got to pass some password as command line argument to something. It would probably be a bad idea to store it in your ~/.bash_history, but clearing the file isn’t desired either. So you need to temporary disable the command history for the current session. You can do it by unsetting the HISTFILE environment variable.

unset HISTFILE

The result is that while the session is active you can access the history as usual, but it won’t be saved to the disk. History for other sessions, will behave as usual.

8 thoughts on “Temporary Disabling Bash History”

  1. In zsh and maybe in bash you could do something similar by adding a space before the command. It will remain in memory for one command and won’t be written to disk.

  2. Just checked the space thing in bash and it seems that it doesn’t work. The command is remains in memory for the whole session and then it’s written to ~/.bash_history (along with the preceding space).

    Nonetheless, if it works on zsh, it’s great as it’s simpler and more intuitive then messing with environment variables.

  3. Add “ignorespace” to the HISTCONTROL environment variable and you should get similar results to zsh.

  4. You’re right, it seems like a good way to do it which is compliant with other shells as well.

  5. Prefixing the command with a space with ignorespace on is great for one-offs, but it’s not so great when you need to build up a complicated pipe, because the command never gets added to the history at all. Unsetting HISTFILE prevents the new history being written to disk but does put subsequent commands into the bash session’s history, so you can edit them within that one session.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.