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:
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:
You'll get a pretty long list of the commands you've executed in the past. Well, my history looked something like this.
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.
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:
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.
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
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.
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.
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.
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.
||Location of the History file (usually ~/.bash_history)|
||The number of lines that the history command can retrieve (500 or 1000)|
||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
This will stop recording history as you enter into a shell.
Alternatively, you can clear the values of
$HISTFILESIZE (change it to zero) to record no history.
These can be added to
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.
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.
Also, check out what a login shell is to understand a bit about the shell and its related files (profile and RC).
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!