[NTLUG:Discuss] keeping your bash history.

Lance Simmons lance at lsimmons.net
Fri Aug 29 16:09:37 CDT 2003


* Richard Geoffrion <ntlug at rain4us.net> [030826 21:51]:
> 
> Is there a way to force a write to the bash history as one exits each
> shell so that the entire history of all bash sessions are recorded?
> Could this be automated in the logout/logoff process?

 history -w <filename>

saves your current history to a file.

 history -r <filename> 

reads the history from a file into the current shell.

 .bash_logout

executes whatever command you want when you logout.  Unfortunately,
usually you don't logout from shells, you "exit".  So you need to have
the shell run the command in .bash_logout whenever it exits.  Adding the
following to .bashrc should (according to the O'Reilly book _Unix Power
Tools_) do it:

# case "$-/${ENV_SET:-no}" in
# *i*/no)
#     # This is an interactive shell / $ENV_SET was not set earlier.
#     # Make all top-level interactive shells read .bash_logout file:
#     set -o ignoreeof
#     function exit {
#         . ~/.bash_logout
#         builtin exit
#     }
#     ;;
# esac

With that in place, you can have each shell upon logout save its history
to a unique history file (e.g. a filename with a timestamp, such as
.history-030829150115).

Then you could set up a cron job to cat the history files, sort and uniq
them, and create a new, master .bash_history, e.g., 

	cat .history* | sort | uniq > .bash_history

As your master history file gets larger, you could edit it until you
have a file that only contains useful commands you might actually want
to re-use.  You could also save that master list in a special file, and
have every new shell source it.

Before I got _Unix Power Tools_ a couple of weeks ago, there's no way
I'd have been able to help you.  I got a used copy for $20, and it's the
best book purchase I've made all year.

-- 
Lance Simmons



More information about the Discuss mailing list