3

I'm running KSH on RHEL 2.6 on x86. KSH version is:

sh (AT&T Research) 93u+ 2012-08-01

Switching to Bash or back to Tcsh (which I happily used at my old job) is not possible.

The first annoying thing I noticed about KSH is that sometimes my history from different terminals (I have many terminals open at a time) got mixed together when scrolling through history with the up key. I then learned that there's a single HISTFILE shared by all my terminals. Fine, but still weird that they weren't always getting intermixed. I also noticed the HISTFILE wasn't always updated immediately. Eventually I decided that KSH was probably buffering some command history and periodically flushing the contents to HISTFILE. I couldn't find that behavior documented anywhere but it seems like a reasonable explanation.

I really want different history for each session, so I added this to my .kshrc file:

HISTFILE=~/.hist.$(tty | sed 's;.*/;;')

which is supposed to give a unique HISTFILE per terminal. It does that, which is great. But...these HISTFILES seem to periodically get cleared. Most of mine have a file size of 0 bytes right now.

Update: The individual HISTFILEs for all my open terminals are being cleared every 10 minutes. Like clockwork.

Anybody seen this before???

1

Yes, ksh93 writes its command line history to file as you go, while bash writes it when the shell exits. Digging though some source code, it seems that the in-memory history buffer is 4k (HIST_BSIZE in the code), but I'm not able to figure out whether it flushes this when it's full or after a certain number of commands (whan may HIST_MARKSZ be doing in this code?).

And yes, many interactive sessions share history if they use the same HISTFILE setting. This is documented (my emphasis):

Command Re-entry.

The text of the last HISTSIZE (default 512) commands entered from a terminal device is saved in a history file. The file $HOME/.sh_history is used if the HISTFILE variable is not set or if the file it names is not writable. A shell can access the commands of all interactive shells which use the same named HISTFILE.

The history file will be kept to the size specified in HISTSIZE but it will not be truncated periodically as you experience. What's happening for you is, I think, that since you use tty to set a unique HISTFILE for each interactive shell session, exiting and restarting interactive sessions may or may not pick up the "correct" history file depending on what TTY the shell is connected to.

If a new TTY (never used with a session before) is allocated for a shell session, it's history file will naturally be empty.

  • Nah, it has nothing to do with exiting or restarting sessions. The history files get cleared every 10 minutes, no matter what. – Matt Feb 23 '18 at 15:00
  • 1
    @Matt I can't reproduce that with the same version of the shell unless I have some cron process or similar to clear out the history file. – Kusalananda Feb 23 '18 at 15:15
  • Yeah I've been wondering if there's something going on in the background. No idea what else could be causing this. Thanks. – Matt Feb 23 '18 at 15:46
  • I'm trying an experiment where the history file doesn't have the leading dot. I could imagine some other tool that produces .hist files for some reason, and it clearing things out. We'll know in 10 minutes... – Matt Feb 23 '18 at 15:49
  • Without the leading dot, the files still seem to get touched every 10 minutes (timestamp updates) but the contents are preserved. Will keep an eye on it over the next few days.... – Matt Feb 23 '18 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.