Keeping history on a per shell PID basis, solution?

16 views
Skip to first unread message

Nicholas Papadonis

unread,
Mar 21, 2020, 12:47:39 PM3/21/20
to Korn Shell
I would like to keep a directory of shell history files, one for each shell session.  For instance, if there are two shell sessions, there are two history files.  I would like the ability to search these files and purge histories of past sessions.  Does anyone have a solution?

So far my solution:

.kshrc
export HISTFILE="$HOME/.hist/history.$$"

# delete history PID files that are not active
function purge_hist {
    for i in $(comm -23 <(sort -n <(ls $HOME/.hist/history.* | sed "s#$HOME/.hist/history.##"))  <(sort <(for i in $(ps -ex -o pid | sed 1d); do echo $i ; done))); do echo "$HOME/.hist/history.$i"; done | xargs rm -f
}

# ksh history files are binary
function view_hist {
    strings -n 1 $0
}

This solution is not optimal.  There could be two invocations of the shell where the PID is the same.  Therefore there must be an additional identified prepended to the history files indicating the session of the OS.

Any suggestions appreciated.

Thanks

Andras Farkas

unread,
Mar 21, 2020, 4:32:40 PM3/21/20
to Korn Shell
I'm about to sleep, and I'll do more research when I wake up, but you may want to look into this utility which isn't in POSIX but exists on many OSes:
It's guaranteed to give you a fresh filename even when two PIDs are the same.
Reply all
Reply to author
Forward
0 new messages