Tips

how to make bash handle your history intelligently

date added/updated
14/Dec/2006
keywords
bash history bash_history append PROMPT_COMMAND
example

The Bash shell has some pretty bad default history settings. For example consider the following scenario:

  1. you start up two terminals/shells/whatever.
  2. you use the first of of them for hours, typing hundreds of commands.
  3. occasionally you open another terminal to run another command, and close it.
  4. hours later, you are finished so you exit the first terminal.
  5. you see the second terminal you created (which you never got around to using) and you close it.a

By default, in the above situation, you will be left with the EXACT SAME history as when you started working, except for a single extra line at the bottom which says "exit".
Every command you typed in those two hours will be gone.
The reason for this is that the second shell you created read the history file when it was started, and then when it was exited (in the final step of the example) it WROTE BACK the history file with only one change - the 'exit' command which you just typed in it to close the terminal. Even though your other shell would have written back it's version of the history (with all the commands you had typed), the file was just overwritten.

The solution is to

  1. tell bash to update the history file after EVERY command you execute
  2. tell bash to just 'append' lines to the history file.

There are various ways to do this, most of which involve getting bash to run "history -a". The way I do it is by setting:

	  PROMPT_COMMAND="history -a"
After doing this, every command you run will be appended onto your history file the next time that bash prints the PS1 prompt (so if you run something in the foreground that might not be straight away).

The end result of this is that your history file becomes a history file with interleaved commands from different shells.

A final important step you probably should take is setting HISTFILE to an alternate file, very late in your .bashrc, ie:

	PROMPT_COMMAND="history -a"
	HISTSIZE=10000
	HISTFILESIZE=10000
	HISTFILE="$HOME/.bash_history_big"
Without setting this, there are various times when bash will get started without having fully read your bashrc and will run with the default history settings which include HISTSIZE and HISTFILESIZE of only 500. The default ~/.bash_history file will get truncated to 500 lines. You should consider putting the HISTFILE line at the end of any other history setting in your bash configs, so that if there is ANY error which results in those settings not being read, bash will only stuff up/truncate the default .bash_history file and your .bash_history_big file will be safe. Of course this is irrelevant if you haven't set HISTFILESIZE up to a higher number. I use 10000 or so.