How To Clear The Terminal History (Bash Shell)

The commands ran in a Bash shell are kept in the history file, allowing users to easily re-execute frequently used terminal commands or to troubleshoot issues that have occurred. This article explains how to clear the history of the commands you run in the terminal when using Bash shell, which is used by default on most Linux distributions.

The shell history for Bash is kept in a file called .bash_history in the home directory. When you exit Bash (e.g. when you close a terminal window), the commands you ran in that session are appended at the end of the Bash history file.

Terminal history - Bash shell

There are cases when you may want to clear the terminal (Bash shell) history, if for example you don't want others to see your previously ran commands, or maybe you've typed your password in the terminal in clear text.

If you want to completely remove all your Bash history, you can open the .bash_history file (this is a hidden file in your home directory, so press Ctrl + h to show hidden files) with a text editor, remove everything from that file, then save it. You can also remove the .bash_history file and re-create it.

If you prefer to completely remove all your Bash history from the command line, this can be done using a variety of commands, like this one:

cat /dev/null > ~/.bash_history

However, it's important to note that while this command clears your shell history, the command used to clear the history will now be in your shell's history. 

Also, in case there are multiple Bash instances (for example multiple terminals or terminal tabs) running, the commands ran in those instances will be saved to the history file, so you may want to close them before clearing the shell history.

Completely clearing your Bash shell history may not be what you want in some cases. Maybe you only want to clear the history of the current shell, or only remove some lines from the shell history. Here's how to do that.

Clear the history of the current shell only:

history -c

Remove only some lines from the shell history. Start by typing history to display your shell's history (this displays the history with line numbers), then delete a particular line using:

history -d LINE_NUMBER

Replace LINE_NUMBER with the line that you want to remove from the shell history.

You might also like: Bash History: How To Show A Timestamp (Date / Time) When Each Command Was Executed

You can also clear the history for a specific range of command. For example, to remove the commands from line 100 to line 200, use:

history -d 100-200

Alternatively, you can open the .bash_history file with a text editor (.bash_history is a hidden file from your home directory so open a file manager in your home directory, then press Ctrl + h to show hidden files) and remove any commands you don't want to keep in your shell's history.

You might also be interested in: How To Change The Default Shell In Linux (Bash, Zsh, Fish, Etc.)