How to change the number of commands stored in Bash History?

The 2 environment variables which majorly affects the Bash History setting of saving the last run commands are HISTSIZE and HISTFILESIZE. The post is a short howto on how to change the number of commands remembered in the bash history.

 

HISTSIZE V/s HISTFILESIZE

The difference between HISTSIZE and HISTFILESIZE is that HISTSIZE limits the number of commands shown by the command history while HISTFILESIZE limits the number of commands which can be saved in $HISTFILE.

 

When one exits the bash, if there are more than $HISTSIZE number of commands which have been executed in the single bash session, the contents of $HISTFILE will be replaced by the $HISTSIZE number of commands. If there are less than or equal to $HISTSIZE number of commands in the bash session, these commands will be appended to $HISTFILE as long as $HISTFILESIZE permits.

 

If the number of commands to be appended to $HISTFILE plus the current existing number of commands in $HISTFILE is greater than $HISTFILESIZE, the oldest commands in $HISTFILE will be removed to ensure the latest commands are kept.

 

How to change the number of commands remembered in bash history

 

In bash, by default, the history commands are recorded in ~/.bash_history file. You can specify a different file other than ~/.bash_history by using the environment variable HISTFILE. The number of commands to be remembered in the history can be specified by the environment variable HISTSIZE. For example, add the following line to your .bash_profile:

# vi ~/.bash_profile
export HISTSIZE=500

 

Then exit the current shell and launch a new one Or you can run the .bash_profile file for the changes to take effect. The number of history commands to remember will be changed to 500.

 

# . .bash_profile

 

Changing the number of commands remembered in bash history in the current shell

 

One can also run the following command to force to append the history commands to $HISTFILE even though there are more than $HISTSIZE number of commands which have been executed in the bash session:

# shopt -s histappend

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)