Clearing Your Bash History

Published:
Most computer savvy people have heard of GNU Linux and why it's either good or bad for them. A point that often comes up when GNU Linux is discussed is the fact that command line interface (CLI) knowledge is part of the skills required to operate a GNU Linux computer. This is a valid argument as graphical user interface (GUI) based applications on most GNU Linux distributions (i.e. Ubuntu, Fedora) are still not truly at the point where they can be considered replacements for direct CLI intervention. GUI applications are often simply front ends for CLI utilities and as such often do not truly capture all the power and extensibility of the latter.

The Bash Shell is often referred to as CLI. It is in fact only a single type of CLI, with its own command syntax rules. There are many other types of CLI implementations, but in GNU Linux the Bash Shell is usually the go-to CLI interface.

The Bash Shell is used to directly interface with all CLI based applications in GNU Linux. It also has its own commands which determine the way Bash Shell itself operates. These commands determine things like: how long of a history Bash Shell keeps, whether a user may use piped commands, keeping track of user assigned aliases for often used commands, etc. The Bash Shell can be the only place a user ever operates in as it provides all the necessary tools for day to day tasks like e-mail, internet access, word processing applications, and pretty much anything else you would normally see on a Windows and/or GNU Linux user's screen.

To open the Bash Shell and start playing with it, you will need to know where it is. It is usually located in a system tools or accessories menu branch and is referred to as the Terminal. This nomenclature is a call back to the olden days when an operator would sit at a CLI terminal and enter commands.

Sometimes during your day to day CLI activities you're forced to write passwords directly into your CLI of choice due to poor programming practices, poor user practices (if you're me), or simply because you don't have time to be the James Bond of the CLI. In such cases you need to make sure that the password doesn't stay logged on your system.

Everything you type (that's not intentionally obfuscated by the application or OS) into your CLI is usually logged. I use bash for my CLI needs. In bash all commands written in the CLI are stored in a file located in

~/.bash_history

Open in new window


or

/home/(username)`_history

Open in new window


If you do a

tail ~/.bash_history

Open in new window


you will see all the commands (up to around 200 by default) that you've written into your terminal. This is usually very helpful because it lets you retrace your steps if something breaks, and allows you an easy way to recall long commands to the screen (by pressing the up arrow several times).

Sometimes you need to erase your bash history because it may contain important information. This is very easy to do. The first option is to run

history -c

Open in new window


This will clear your whole history file. What if you want to remove a specific entry in your history file? You're in luck! the history command gives you a chance to do that

history | tail

Open in new window


This pipes the output of your perhaps long history into a readable format with the latest commands first. Now you have to find the number of the command you want to remove. Get it and remember it.

history -d (number)

Open in new window


That's all! This should give you a reasonable assurance that your commands will not be visible to would be root pirates (a.k.a.: hackers arrrrrrrg!), smart dogs, and curious siblings.
1
3,256 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.