Skip to main content
How To

How to Clear the Contents of Bash History

Learn to clear bash history, either all of it or selected entries in this tutorial.

Pranav Krishna

Every command you enter in the terminal is recorded in your shell's history.

This is more of a feature since it allows you to look for commands you ran in the past and run it again.

If you want to clear the bash shell history, use this command:

history -c

That's simple, right? Let's see about this bash history in more details.

Checking your Bash History

The history of all the commands in Bash is stored in the file .bash_history in your home directory. Generally, the last 500 or 1000 commands are currently stored in that file.

If you haven't checked your history in a while, now is a great time to do so. Just type this:

history

You'll get a pretty long list of the commands you've executed in the past. Well, my history looked something like this.

Output of history in my system
The output of history in my system

Though your last index number may vary, your history file stores only a certain number of commands. Navigate to the beginning of your output, and the first index might not always be 1.

💡
The variable $HISTSIZE tells how many lines your bash history file can contain. It's usually 1000, but you can change this to suit your needs.

How bash saves history

The history of the current session will get stored in the memory for the time being. The saved commands are written to the ~/.bash_history file upon exit.

This is why if your system is forcefully shut down or anything similar happens, you might find the history of that session to be missing. Now you know ;)

Clearing the History

There are a fair few ways to clear your bash history. The most common way is to use the history command. To wipe out the history in your current session, run this:

history -c
history of current session cleared
History of the current session (cleared)!

When you log out and log in again to check the history, you don't get the commands from the cleared session. This is a basic one.

History from previously cleared session is missing
History from cleared session missing (number starts from 238 rather than 245)

Searching particular entries in the history

Searching for entries is easy even if a part of the command is known. You can just redirect the output of history to the grep command.

history | grep command_to_search
Searching for the term 'bash' in History
Searching for the term 'bash' in History

I used a method called pipe redirection to redirect the output of the history command as input to grep. This comes in very handy at times.

Pipe redirection in Linux: Named and Unnamed Pipes
There are two kinds of pipes in Linux: named and unnamed. Here’s a detailed look at pipe redirection.

Removal of entries from the history

Let's say you don't wanna remove the entire history, but rather a few commands from it. There's an option -d just for that.

What it does is, deletes the particular entry with the serial number given.

history -d serial_number

Use the above searching method to check for the number, and run the command.

Removing the 249th entry in history
Removing the 249th entry in history

If you would like to make that change permanent, add -w to the arguments as well.

Note that the index values also accept negative values, so you can remove lines from the bottom as well. With the last command counting as -1, you can remove it in reverse.

Removing the 4th entry from the bottom
Removing the 4th entry from the bottom, which is 300 from the top

Stop Recording History entirely

It's possible to entirely stop recording history. That requires modifying certain files and variables. Let me explain the steps.

There are a few variables to understand to know what you're doing.

Variable Explanation
$HISTFILE Location of the History file (usually ~/.bash_history)
$HISTSIZE The number of lines that the history command can retrieve (500 or 1000)
$HISTFILESIZE Number of lines in the history file (1000 by default)

We can stop recording the history (writing it to the file on the hard drive) by unsetting the $HISTFILE variable. History is not recorded after running this command.

Yeah, to make this permanent, add this line at the end of ~/.bashrc file.

unset HISTFILE

Add to ~/.bashrc

This will stop recording history as you enter into a shell.

Unsetting $HISTFILE stops recording history
Unsetting $HISTFILE stops recording history, but is valid only inside a shell

Alternatively, you can clear the values of $HISTSIZE and $HISTFILESIZE (change it to zero) to record no history.

These can be added to ~/.bashrc.

export HISTSIZE=0
export HISTFILESIZE=0

Add to ~/.bashrc

If you wanna clear history at logout (but preserve the current shell history until exit), let me introduce you to ~/.bash_logout. You can add any one of the above lines to the bash_logout file, and history gets cleared upon exit.

History is cleared upon exit of the shell
History is cleared upon exit of the shell

This is all there is you should know about clearing history in Bash. There are other use cases of the history command itself, but this article has gotten lengthy enough to skip them.

If you would like to know more about the usage of history command, I'd recommend you to read this article.

Using History Command in Linux [Beginner’s Guide]
Everything you type in the terminal is stored in the shell history. Learn how to use command history in Linux in this introduction to history command.

Also, check out what a login shell is to understand a bit about the shell and its related files (profile and RC).

What is Login Shell in Linux?
You probably are aware of the shell in Linux. This is where you enter commands and execute programs. But what is this login shell and how is it different from the regular shell?

That's all about the article. While you exit, here's a little pun about clearing the command history:

Why did the sysadmin clear their Bash history? To cover their tracks and make it look like they knew what they were doing!

Pranav Krishna
@theproudlinuxer Tamil Nadu, India