bash history tricks

I used the following custom PS1 to show a sequence ID for my next command on the prompt:

[\033[01;34m]! [\033[01;34m]\w $ [\033[00m]

For the past few days, I found it always start with 1000. Then I realized there was a upper limit of 1000 commands to be stored in the history file, and I wanted to raise that limit.

This wiki contains a really great summary of the important items relevant to history management in bash. Take home messages include

  1. HISTFILE specifies the file to use for history storage.
  2. HISTFILESIZE limits how many entries will be persistentized in the file system. The file will not grow beyond that. Upon set, the file is truncated immediately if if it is larger than this size.
  3. HISTSIZE limits how many records to be kept in memory. On a busy day, if your session grows beyond that number of entries, it will be thrown away in memory. This can be larger or smaller than HISTFILESIZE.
  4. PROMPT_COMMAND is the command to run before showing prompt. It is a trick that you can use to show mbox or calendar event notification, for example.
  5. shopt -s can be used to set bash options after the session has started. One option, histappend sets the history to be appended rather than overwrite on every session termination. Like someone else, this does not have any effect on my machine.
  6. history -n can be used to load “new” entries into the memory. It might not work reliably if you switched HISTFILE. You can use history -r instead to discard the entries in the memory and restart from those in the persistent file.

A novel use of these tricks cay be keep a HISTFILE for each month. To avoid an empty start on the 1st of each month, you can copy the last 1000 lines from last month to start, for instance.