How to Review Bash History

When working in a Linux command-line interface (CLI), you may find yourself wondering exactly what commands you ran during the course of your workflow. Fortunately, most Linux operating systems store your commands in a bash history file, which is a stored list of all commands a given user has entered into the system. By reviewing this list, users can troubleshoot input errors or mistakes made while performing command-line operations. In this article, we will explore how to review bash histories and discuss the importance of the information contained within.

Topics Include:

Learn how to review bash history to take more direct control over your Dedicated Hosting server!

What is Bash History?

Bash history is simply the history of commands issued in bash. Bash is an abbreviation of “Bourne Again Shell”, a type of user-interface or shell that allows users to directly interact with computer operating systems. This specific type of shell was developed for Unix systems and is still in use today on most modern Linux operating systems. A command run in bash is generally referred to as a bash command. Each of these commands are stored in the bash history, which can be reviewed in a list format using a simple Linux command. In the next section, we will discuss how to review this information.

Reviewing Bash History

To review your bash history, you can use the the following command:

history

This should output a numbered list of all past commands entered into the system, in chronological order. If you wish to expand the amount of commands stored in the bash history, you can do so by modifying the .bashrc file. This file dictates how a given user’s bash profile operates, including how bash history is stored. In this file, bash history is stored based on two parameters. 

The first parameter is HISTFILESIZE, which dictates how many commands are stored in the history file in total. The second parameter, HISTSIZE, dictates how many commands are stored in a given session. By increasing these values in the .bashrc file, you can increase the number of commands stored in the bash history. 

Now that you have a list of previously-issued commands, you can call them using the number assigned in the history. For example, if I wanted to rerun the command listed in the history as entry 46, I could call that command as follows:

!46

This will run the command that is associated with the 46th entry on the bash history list. By calling commands in this way, users can streamline repetitive command-line operations. 

By default, bash history is stored at the end of a bash session. In the event of multiple concurrent sessions, the bash histories may end up overwriting each other, with the last connected session being the only history that is saved. To get around this, we can set the bash histories to append, instead of overwrite. This can be done using the following command:

shopt -s histappend

If done correctly, bash histories should be appended to the file for all sessions, even in the event of concurrent bash sessions. 

If you wish to clear the bash history for security reasons, you can do so with the following command:

history -c 

This will clear all commands from your bash history permanently. Please be aware that deleted histories cannot be recovered. 

As you can see from these examples, reviewing bash histories can be done with a handful of simple commands. These characteristics make bash histories a vital part of any Linux workflow or troubleshooting operations.

Was this article helpful? Let us know!