If you are a Linux user who frequents the command line, you will most likely have heard of the bash history. This is a special system that keeps a history of all commands run from bash. This has many pros and only a few cons. One of the cons is that, if you are logged onto your machine, someone can see your command line history. There are plenty of situations where you might not want this to happen. In that case, you have to delete your history.
Let's take a look and see how this is done.
Before we start...
There is always a "before" right? In this case I wanted to touch base with everyone to make sure it is know how you can work with the bash history. There is one way the bash history is immediately useful to the user. The bash history makes it easy for recalling the last commands run. If you open up a terminal window and start hitting the up arrow on your keyboard you can run through all of the commands contained in your bash history.
So all you need to do is keep hitting the up arrow until you see the command you wanted to run. This will save you time by keeping you from having to type long commands over and over. It can also help you troubleshoot by showing you what commands you have recently run, which will allow you to deduce what has happened to your machine.
How to delete the history
There are two steps to deleting your bash history. The first step is to delete the ~/.bash_history file. This won't work completely because there will still be a history stored in RAM. So, to get rid of THAT history you have to run the command history -c. Once that command is issued, your bash history will be completely clear.
Not keeping your history
What if, for whatever reason, you do not want your bash history to be retained at all? You can do this. You can do this for either all users or specific users. To turn off bash history for all users you would need to issue the command:
echo "unset HISTFILE" >> /etc/profile
To turn off bash history for a single user issue the command:
echo "unset HISTFILE" >> /home/USER/.bash_profile
Where USER is the user you want bash history turned off.
Once you've done either of the above, bash history will be turned off for good.
Remember, you can control the size of your bash history by editing the lines in your ~/.bashrc file:
Change the entry to match the amount of commands you want to retain in your history.
Bash is an amazing and powerful tool for any and all Linux users. Although you do not have to ever touch the command line, in modern Linux desktops, you may someday have to use the command for either a server or remote access to another desktop. For whatever the reason, you will want to either take advantage of the bash history.Advertisement
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.