Save command history from multiple terminal sessions, and control the size of the history buffer in memory and the history file on disk.
The Linux Bash shell history size & settings are not necessarily configured optimally by default, but there are environment variables and options that are easy to change and can help significantly. The History command is a powerful tool and a great makeshift knowledge base for lazy admins. Unfortunately many Linux distributions default to a very limited command recall environment that not only retains a tiny list of commands, but is also overwritten each time a terminal session exits. The default behavior is great if you only use one session at the time (although it may still be too small). When you launch the session, it reads the saved history into your buffer, and then new commands are appended as you use it. Then when you exit, the complete history from your buffer (old + new) is saved back to the history file. But if you tend to have more than one terminal session open at the same time, then you have probably experienced the frustration of having the command history in the last closed session overwrite all the important history that was in another active shell session and closed first. Luckily, there is a more rational configuration.
Save History from Multiple Concurrent Sessions
The histappend shell option tells bash to append new lines of history to the file instead of overwriting it with your current copy of old + new. There is a subtle difference, but there is a difference, and this is the key to saving all the history from multiple concurrent terminal sessions instead of just the last one closed. You can enable this behavior using the shell option command shopt from the command line, and you can add it to your .bashrc file so that it is enabled by default.
$ shopt -s histappend
Customizing Command History (Recall) Size
You can also modify the HISTSIZE and HISTFILESIZE environment variables to retain more history (or less if that’s what you want 😉 ).
There are many permutations about how this can be done, and there are many, many more opinions about how it should be done.
The HISTSIZE environment variable controls the number of commands to remember in memory, and it defaults to 500. Distributions often set it to 1000, but even that is too small. Per the bash manpage, if HISTSIZE is unset, then no history is saved. So it needs a value unless you are hiding your tracks. I prefer it to be large enough to store everything I do in a session.
The HISTFILESIZE environment variable controls the number of lines saved in the history file itself. Per the bash manpage, it too defaults to 500, but if it is unset, then the file will not be truncated, and that’s exactly what I want: no limit.
There are others you might want to be familiar with like HISTCONTROL which can alter the behavior to ignore duplicates lines or lines that start with spaces, etc., and HISTIGNORE which can contain patterns that shouldn’t be saved at all. See the bash man page for more information.
The following will set the number of lines to remember (in memory) to 1048576, it will “unset” the file size so that there is not a limit to the history saved on disk, and it will configure the shell option to append. There are potential ramifications to having an unlimited history size, and you have to choose the options that are right for you, but this retains everything for all sessions which is exactly the behavior I want. You can add this to your .bashrc or .bash_profile as appropriate.
HISTSIZE=1048576 unset HISTFILESIZE export HISTSIZE HISTFILESIZE shopt -s histappend