For many users who get started with the command line in Linux, there’s a good chance they’re using Bourne Again Shell, or Bash. Bash is the default shell on Mac OS X, and Windows users can use Bash through the Windows Subsystem for Linux.
While Bash generally works just fine with little to no configuration, there are some features that can really make the shell more useful. One of those commands is history.
Looking back in time
If you’ve ever looked at your web browser’s history, you already have a good idea of how history works. Bash’s history stores the history of commands that have been entered on the command line. This can be useful if you’ve ever wondered how you did something earlier.
To see a list of past commands, simply type history on the command line. You’ll see a list of commands (including history) that have been entered during the current shell session.
Bash’s history also provides arrow-key navigation. If you open a Bash shell and press the up arrow, you’ll see the previously entered command appear on the command line.
Bash keeps its history in memory until the shell session is closed, when it will write it to disk (the file is generally located at /home/(user)/.bash_history). You can manually append the current history to the history file using the -a switch like so:
$ history -a
You can also append the history from file to the current session with the -n switch. If you prefer that the history file overwrite the history in memory, use the -r switch. Finally, you can overwrite the history file with the -w switch.
Sending history to the memory hole
While keeping a history of every command you’ve entered can be convenient, just like keeping every message in your Gmail account or leaving your browser history untouched. But like leaving those troves of data alone, they can grow to become security risks. I for one can recall entering passwords on the command line without thinking about it because a previous command failed. For advanced users who make use of SSH or database connections, those commands allow for passing passwords as command line arguments.
For those users who are a little more paranoid about privacy, a Bash history file represents a treasure trove of metadata about how a person uses his or her computer.
Luckily, you can delete your history from memory with the -c switch. But be careful: Clearing the history from memory does not clear the history file. If you remember the -w switch I mentioned earlier, you have the tool for the job. To clear the history file, simply clear the memory with the -c switch then write the (mostly) blank file to disk with the -w switch:
$ history -c
$ history -w
It can get a little tedious having to remember to clear your history file each time you’re done working, but you can also set the variable HISTFILESIZE to zero to prevent additional lines from being written. To keep the changes, you can echo the setting out to ~/.bash_profile:
$ echo “HISTFILESIZE=0” >> ~/.bash_profile
You can delete a single entry in your Bash history with the -d switch followed by the command’s reference line (which is displayed to the left of the list of commands):
$ history -d 42
This is great for when you realize you accidentally typed a password and just need to clear that one goof.
Sometimes keeping a history isn’t such a bad thing, but some people might prefer having a choice about what gets recorded and when. Bash has those people covered too, with a special setting that tells the history recorder to ignore every command that begins with a space character.
In Bash, commands run just the same if they are typed with 10 spaces in front of the command, or none. The setting HISTCONTROL is a list of control settings that Bash uses to control its behavior. If HISTCONTROL is set with “ignorespace”, Bash will omit any command with a preceding space character from its history.
To set HISTCONTROL temporarily, type:
To set it permanently, add it to your .bash_profile file:
$ echo “HISTCONTROL=\”ignorespace\”” >> ~/.bash_profile
While keeping your Bash history free and clear of information may seem trivial, it can be important to the budding developer or anyone using a shared computer. Remember: While other regular users of a Linux or Mac machine can’t read the history file, anyone who can obtain root privileges can. If you’re sending in your laptop for repair or having another untrusted person access to your computer, be sure to wipe your Bash (not just web) history.