While working in a Linux command-line interface (CLI), you might have wondered exactly what commands you executed during your workflow. Thankfully, you can find the commands you used in a bash history file that stores list of all commands a given user has entered into the system. When you review this list, you can troubleshoot input errors or mistakes done while performing command-line operations. In this article, you will learn about reviewing bash histories and the importance of the information contained within.
What is Bash History?
The history of commands issued in bash is known as bash history. Bash is a short-form for “Bourne Again Shell”, a type of user-interface or shell with which users can directly interact with computer operating systems. This shell was developed for Unix systems and is still in use today on most advanced Linux operating systems. A bash command is a command run in bash. The bash history stores each of these commands that can be reviewed in a list format with the help of a simple Linux command.
Ways to Review Bash History
You can review your bash history using the following command:
history
With this command you will get a numbered list of all past commands entered into the system, in sequential order.
You can expand the amount of commands stored in the bash history, by modifying the .bashrc file. This file displays the operating style of a given user’s bash profile, including the way its bash history is stored. In this file, bash history is stored depending on two parameters.
The first parameter is:
HISTFILESIZE
It dictates the total number of commands stored in the history file.
The second parameter is:
HISTSIZE
It dictates the number of commands stored in a given session. When you increase these values in the .bashrc file, the number of commands stored in the bash history can be increased too.
After having a list of previously-issued commands, they can be called using the number assigned in the history. For example, if you want to rerun the command listed in the history as entry 20, you can call that command as follows:
!20
With this, the command that is associated with the 20th entry on the bash history list will run. Users can simplify the repetitive command-line operations.
The bash history is by default stored at the end of a bash session. During the multiple concurrent sessions, the bash histories may lead to overwriting each other and so only the last connected session will be saved in the history. To resolve this issue, set the bash histories to append, rather than overwrite with the following command:
shopt -s histappend
When you do it correctly, bash histories should be appended to the file for all sessions, even during the concurrent bash sessions.
For clearing the bash history for security reasons, use the following command:
history -c
With this, all commands from your bash history will get cleared permanently. Remember you can’t recover the deleted histories.
From the above examples, it is clear that you can review bash histories with a handful of simple commands. Due to these characteristics, bash histories have become a vital part of any Linux workflow or troubleshooting operations.