How to Clear the Bash Command History in Linux

Every time you execute a command in the Linux terminal, the bash shell quietly records it in a hidden log file. This feature is incredibly useful for recalling a long, complex command you typed yesterday by simply pressing the “Up” arrow on your keyboard.

However, this convenience becomes a severe security risk if you accidentally type a plaintext password, an API key, or a sensitive database credential directly into the terminal prompt. If another user gains access to your account, or if a malicious script reads your history file, those credentials are completely exposed.

To protect your sensitive data, you must know how to completely eradicate your command history.

How to Clear the Active Session History

If you just typed a sensitive command and want to immediately wipe the memory of your current active terminal session, you can use the built-in history command.

  1. In your terminal, type the following exact command:
    history -c
  2. Press Enter.

The -c flag stands for “clear.” This instantly erases the temporary list of commands stored in your system’s RAM for the current session. If you press the Up arrow now, nothing will happen.

How to Clear the Permanent History File

While history -c clears the active memory, it does not always delete the permanent log file stored on your hard drive. By default, bash saves your permanent history in a hidden file named .bash_history located in your user’s home directory.

To ensure absolutely every trace of your past commands is eradicated from the disk, you must delete the contents of this specific file.

  1. Execute the following command to securely overwrite the permanent history file with nothing (an empty string):
    cat /dev/null > ~/.bash_history
  2. Press Enter.

This command takes the emptiness of /dev/null (a black hole in Linux) and forces it into the .bash_history file, instantly wiping it clean without deleting the file itself.

The Ultimate Wipe Command

If you have finished a highly sensitive server configuration session and want to guarantee both the RAM cache and the hard drive log are completely sanitized before you log out, you should combine the two commands into a single execution:

history -c && cat /dev/null > ~/.bash_history

Once you press Enter, your terminal will have absolute amnesia. No record of your session will exist anywhere on the machine.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.