Randall wrote regarding '[SLE] BASH History' on Sat, Aug 28 at 10:07:
Hi,
The BASH history question reminded of a small problem I have with this function that perhaps someone can help me with.
I always keep a Konsole window open in which I have four regular interactive BASH shells and one root shell running.
When I log out or shut down, it seems that the four shells running under my user ID clash somehow or fail to coordinate in the way they write the history file, and I end up with only one of those shells' history.
Does anyone know of a way to get all four shells' worth of history saved?
Well, the problem here is described in the bash man page. I know, I know, man pages are too complicated for mere mortals to read (that's the gist I get from the list recently). So, as I consider myself well above mere mortal status, let me copy and paste. :) On startup, the history is initialized from the file named by the variable HISTFILE (default ~/.bash_history). The file named by the value of HISTFILE is truncated, if nec essary, to contain no more than the number of lines speci fied by the value of HISTFILESIZE. When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to $HISTFILE. If the histappend shell option is enabled (see the description of shopt under SHELL BUILTIN COMMANDS below), the lines are appended to the history file, otherwise the history file is overwritten. If HISTFILE is unset, or if the history file is unwritable, the history is not saved. After saving the history, the history file is truncated to contain no more than HISTFILESIZE lines. If HISTFILESIZE is not set, no truncation is performed. In the reading of that section, it becomes clear what's happening. Each of the shells that your kterm starts reads the history file when it starts up. Then, it stores the commands in memory until it exits. At the time of exit, it creates a new history file containing the last 500 entries in memory. What you have is 4 shells that have the same last 500 lines when they start, but then each one adds only to its in-memory list. The lines that are saved, therefore, can only be those that are in the list of the last one to exit, and it has no idea what the other shells might have put in the file when they exited. If you want to solve that problem, the one possible "reliable" solution is to make the history file a named pipe, and run a daemon that manages sorting duplicates, etc. That wouldn't be *too* hard to write, but would be a pain none the less. You could play with the "shopt" builtin to change the history's features (or change variables before startup). The first thing that comes to mind is that you could set "histappend" so that each shell appends to the end of the existing history file rather than overwriting it. That would very likely save you, as the shell should append then truncate, and the other exiting shells (they do exit when you log out) would respect the file lock placed on the file when it's being manipulated by each shell in succession. put a line that says shopt -s histappend at the top of your .profile / .bashrc and see if that helps. Turn on the "cdspell" option, too. It's cool. 'shopt -s cdspell' --Danny, who misspells stuff regularly :)