You must aware of using the up and down arrow keys to scroll through the list of executed commands in your Bash history, but do you realize that there’s plenty more to Bash history than simply repeating commands?
One such feature of the Bash shell that can be changed in your customized settings is the history command, which is affected by some environment variables and some shell options (shopt – a command to set and unset shell options).
[ You might also like: How to Check Linux Commands History by Dates ]
When you use multiple shell windows or multiple sessions at once for the same user from different systems, the way that the history command logs commands can get a bit cumbersome, and some commands may be overwritten by newer ones.
How to Stop Bash Appending History in Linux
To prevent this from happening, you can set the following option in your ~/.bashrc file.
# ignore duplicate entries and don't overwrite the history export HISTCONTROL=ignoredups shopt -s histappend
Once variables are defined, you can use the new values by running the following command.
$ source .bashrc
How to Increase Bash History Size in Linux
Another possible difficulty with the history command is that it can take up a great deal of disk space if you do not have much disk space for your personal files. To prevent this from happening, you need to use three environment variables and they are HISTFILE, HISTFILESIZE, and HISTSIZE.
HISTFILE—/home/<username>/.bash_history HISTFILESIZE—1000 HISTSIZE—1000
- The HISTFILE variable specifies the name and location of your Bash history .bash_history file.
- The HISTFILESIZE specifies the maximum number of commands that can be stored in a history file..
- The HISTSIZE variable specifies how many cached commands a shell session should store in the history file.
Once you outreach 1000 Linux commands, the oldest commands will be abandoned as new ones are saved.
$ echo $HISTSIZE $ echo $HISTFILESIZE $ echo $HISTFILE
By default, each user, including root, has these variables and sizes allocated. If you want to change, edit the .bashrc file in your home directory and set the size variables to 2000 commands as shown:
# control the size of the history file export HISTSIZE=2000 export HISTFILESIZE=2000
To use the new values without logging off and back on again, you can execute the .bashrc file.
$ source .bashrc
You can confirm that your new values are now active for your current shell and any subshell.
$ echo $HISTSIZE $ echo $HISTFILESIZE
How to Avoid Storing Duplicate Commands in History
If you don’t want to store common commands in your bash history, you can use the HISTIGNORE variable to avoid storing common commands such as ls, pwd, and uptime, which are not normally very interesting to audit.
Also, if you do not want to store duplicate commands, then simply use the HISTCONTROL variable that instructs your history to ignore duplicate entries.
Open the .bashrc file in your home directory and add the following entries:
# ignore common commands export HISTIGNORE=”:pwd:id:uptime:resize:ls:clear:history:” # ignore duplicate entries export HISTCONTROL=ignoredups
Once added above variables, make sure to execute the following command to use new values.
$ source .bashrc
The Bash history is a useful tool, but it can be challenging if you aren’t friendly with its options and usage. There are many more options accessible in history. Check the man pages for additional information.