Bash History Size
- Overview of the Bash History
-
Use the Bash
history
Command to View Bash History - Bash History Size
- Modify the Bash History File Size
- Set Unlimited Bash History File Size
- Control the Bash History
-
Save the History to
HISTFILE
Before Logging Off
In this tutorial, we’ll learn about Bash’s history, its size, and how we can change our history size and handle limits. Before moving towards our topic, let’s first discover why we need history in a Bash shell and how we can take it.
Most developers, system administrators, and software application engineers spend much time at the Linux command line. Being human, they make typing mistakes.
Also, many commands have a list of parameters/options (with their respective order). Therefore, it is difficult to remember them in their exact order.
Therefore, history helps us recall the previous command typed in the Bash shell.
Overview of the Bash History
In terms of history, Bash keeps track of the commands, files, and shortcuts. The users can type on the command line with the history
utility.
The history
utility provides beneficial information. It helps to track down how the current system or account changes might have occurred.
Bash has two built-in commands related to the Bash history:
history
- This command provides a list of previously used commands from your Bash history, and it also allows you to modify Bash historyfc
- This command performs all tasks of thehistory
command and allows the execution of the command.
All the information about Bash history will be saved in the ~/.bash_history
file for Bash users, while it might be just .history
for other users.
Let’s start with our history
command and see how it works.
Use the Bash history
Command to View Bash History
If you want to view your entire history, you can write the history
command in the shell. It will list the whole history from the history file.
$ history
The output will look like this:
1 ping google.com
2 sudo apt update
3 ping google.com
4 java --version
5 sudo apt install openjdk-17-jre-headless
6 sudo apt-get install gcc
7 sudo apt-get install gpp
8 sudo apt-get install g++
9 sudo apt-get install mingw-w64
10 sudo apt-get install codeblocks
11 codeblocks
12 sudo apt update
13 sudo apt update mu-editor
14 sudo apt install mu-editor
15 sudo apt update
16 python3 --version
17 clear
18 snap find pycharm
19 sudo snap install pycharm-professional --classic
20 clear
21 cd desktop/python
22 cd desktop/python task5.py
...
There are also other helpful Bash history
commands which we will not explain, but you can read them from this website.
The fc
command in Bash in Linux is also known as the fix
command. Mainly, it is used to modify multi-line commands.
The parameter -l
with the fc
command shows the previous command history.
Let’s look at the following example of the command and its output:
$ fc -l
302 284 ./a.out
303 285 gcc execve1.c -o e1
304 286 gcc execve2.c
305 287 ./a.out
306 288 $ sudo snap install whatsdesk
307 289 sudo snap install whatsdesk
308 290 cd ..
309 291 cd CodeForces/
310 292 pwd
311 293 history
312 294 1 ping google.com
313 295 ls
314 296 wc Test.c
315 297 fc -l
316 298 ls
317 clear
Bash History Size
If you talk about the Bash history file, you think there is also some data. The Bash history file’s default is 500
to 1000
.
It is the maximum number of entries in the history file. But it is not permanent, as we can configure our history size to what extent we want by using different commands.
Let’s explore them.
Configure the Bash History
To configure the Bash command history, you must make changes to the ~/.bashrc
file. The ~/.bashrc
file can be updated using the following nano
command:
$ nano ~/.bashrc
Once you have modified, press Ctrl+O followed by Ctrl+X to save and close the nano
editor. Then, run the following commands to reload the Bash settings:
$ source ~/.bashrc
or
$ . ~/.bashrc
Both commands will do modifications in the Bash history file.
Modify the Bash History File Size
The variables HISTSIZE
and HISTFILESIZE
are related to the Bash history. The variable HISTSIZE
has a count of maximum Bash commands to be cached, whereas the variable HISTFILESIZE
has a count of top commands stored in the history file.
By default, we can keep at most 500 commands in the history file.
We can view these values by printing these variables; the output can vary on different machines/configurations. Here is one sample run and related result:
$ echo $HISTSIZE
$ echo $HISTFILESIZE
1000
2000
We can configure these values by adding the following lines into the file ~/.bashrc
:
$ HISTSIZE = 6000
$ HITFILESIZE = 6000
Again, we can print these variables to check the values:
$ echo $HISTSIZE
$ echo $HISTFILESIZE
6000
6000
Set Unlimited Bash History File Size
The previous section discusses the method to modify the history size in Bash. However, sometimes it is handy to let it set to an unrestricted size. How will we do that? Let’s explore!
You can also set your Bash history size or file size total by using following these steps:
-
Change the Bash history file. As you can read in the StackOverFlow, there are some issues with the Bash history file as it gets truncated in some cases.
We can overcome this issue by changing the
HISTFILE
environment variable to use a different directory.export HITSFILE= ~/.history
HISTFILE
refers to the path/location of the history file (the default is~/.bash_history
). -
Increase the history size. Set the
HISTFILESIZE
andHISTSIZE
variables to an empty string to make the Bash history size unlimited.$ export HITSFILESIZE= $ export HITSIZE=
-
Copying existing history. Although we’re using a new history file, we shouldn’t lose the previous file commands, so we’ll copy the contents of the old file to the new one.
$ cp ~/.bash_history ~/.history
Control the Bash History
You may exclude commands from Bash history to avoid output noise or security reasons. You can do this using the history control command HISTCONTROL
, which controls what history is stored.
You can specify it to ignore duplicate entries and entries with leading white spaces in your ~/.bashrc
file.
ignorespace
– This eliminates commands that begin with a space history list.ignoredups
– This eliminates duplicate commands.ignoreboth
– This enables bothignoredups
andignorespace
.erasedups
- This eliminates duplicates from the whole list.
$ export HISTCONTROL=ignorespace:ignoredups
You can also control commands to be ignored using the variable HISTIGNORE
. It is a colon-separated list of patterns in which we can specify all the commands we want to omit from history.
For example, if you want to ignore basic and most frequent commands (e.g., ls
and pwd
) in the history list, write the following line in the ~/.bashrc
file:
$ export HISTIGNORE="ls:pwd:"
You can do more with Linux Bash command history than repeat the old commands.
Save the History to HISTFILE
Before Logging Off
The commands are written to the Bash file only after the user logs off. However, the write option is available with the history
command to write the history in the file before logging off.
The syntax is:
$ history -w
This command will write all current session command history to the HISTFILE
. After this, the command script will be available for future use.
In this article, finally, we have covered Bash history, history size, and file size or how we can modify the Bash history and its size to a limited or unlimited range.