Disable Current Shell Session Input History

Sometimes you need to run commands that shouldn’t persist in your shell history; credentials, sensitive commands, or one-off experiments on remote machines.

The input history, aka history, is a major productivity boost, but we need to understand that the history buffer is not committed until you log out, so everything you see when running the history command is not yet written to disk.

To prevent the current session’s history from being saved, unset the HISTFILE environment variable. Although removing history completely is not a good practice.

unset HISTFILE

A better option would be ignoring or deleting a specific line. To avoid saving individual commands to the input history, prepend a blank space before each command. To do this we need to set HISTCONTROL to ignorespace.

export HISTCONTROL=ignorespace

Finally, to simply delete specific commands from history execute:

history -d <history-number>

Use unset HISTFILE when the entire session is sensitive, HISTCONTROL=ignorespace for selective exclusion during normal work, and history -d to remove commands after the fact.