capturing bash history

Aaron Crane perl at aaroncrane.co.uk
Tue Jun 12 12:47:49 BST 2007


Nigel Hamilton writes:
> I'd like to keep a history of all shell commands across all the xterms I'm
> using but the ~/.bash_history only holds history from one terminal.  Any
> ideas on interleaving the shell history from multiple terminals into one
> file?

This is the magic:

  # Append to the history file on shell exit, don't overwrite (so multiple
  # exiting shells don't race to scribble over your saved history)
  shopt -s histappend

  # Keep lots of history around
  export HISTSIZE=1000000 HISTFILESIZE=1000000

  # Store history in a different file, so it won't get overwritten if you
  # don't have these settings
  export HISTFILE=~/.bash_history_safe

  # Write unsaved history immediately before emitting each prompt
  export PROMPT_COMMAND='history -a'

I stole most of that from Smylers:

  http://www.ukuug.org/events/linux2003/papers/bash_tips/

> p.s. it would be great if I could get this to work across multiple machines
> too --- but I'll settle for one machine to start with

I've never wanted that, but it's essentially going to be a matter of copying
history files from place to place, and appending their deduped contents to
the desired history file on each machine.  I suspect the trickiest bit is
working out when to do it; on each prompt sounds painfully slow, for example.

-- 
Aaron Crane


More information about the london.pm mailing list