In this lesson, you will learn how to configure your shell to control how many commands are remembered in memory and stored in your permanent history file.
The Memory vs. Disk Distinction
By default, your shell tracks commands in two ways: it keeps a "buffer" in your current session's memory, and it writes those commands to a file (usually ~/.bash_history) when you exit. To manage this effectively, Bash uses two primary environment variables: HISTSIZE and HISTFILESIZE.
HISTSIZE: This controls the number of commands stored in your current memory buffer. If you set this to 500, your shell will only remember the last 500 commands you typed in the active session.HISTFILESIZE: This controls the size of the persistent file on your hard drive. This prevents your history file from growing indefinitely and consuming unnecessary disk space.
Configuring Your Buffer
You can view or modify these settings in your .bashrc file. For instance, if you want your shell to be very conservative with memory, you might add these lines:
# Keep only 1000 commands in the current session's memory
export HISTSIZE=1000
# Keep only 2000 commands in the permanent history file
export HISTFILESIZE=2000
After updating these variables, you must run source ~/.bashrc to apply the changes to your current terminal.
Why Manage This?
If your HISTSIZE is too high, you might encounter performance issues during session initialization. Conversely, if it is too low, you will lose access to recent commands after only a few minutes of work. By balancing these two values, you ensure that your command history remains a useful, searchable archive rather than a bloated, sluggish file.
It is also common to use HISTCONTROL to manage duplicates. Setting export HISTCONTROL=ignoredups tells the buffer manager not to store the exact same command twice in a row, keeping your history clean and easier to navigate when using the up-arrow or Ctrl+R.
Key Takeaway: Use HISTSIZE to limit the commands held in active memory and HISTFILESIZE to regulate the size of your permanent disk-based history log.