1

I have read that ksh, in contrary to bash, flushes its history after each command. This may be good for many, but for me, it drives me nuts!

I have several windows opened that are opened for their own tasks, and this history being shared messes up with specific session history of commands.

Is there a way I could disable this flushing, so the history could be appended to the command history at the logout time only?

Or, maybe there is a way to request history not to refresh command line history each time?

2
  • 1
    One solution is to set $HISTFILE to a different file in each window, so each has its own history. You need to set this before starting ksh, I believe.
    – meuh
    May 30 '18 at 19:23
  • @meuh - I had this idea for a moment but then it will be a mess with leftovers from shells being killed, and so on... Before I embark on this idea I am looking for a simpler one - like an option for history/ksh... At the end, I can give up on ksh and run bash.
    – Grzegorz
    May 30 '18 at 19:43
0

I used to have multiple shells running in screen. In my .khsrc I would customize several settings, including $PS1 and $HISTFILE (and include the basename of the tty device).

This was relatively easey to reconstruct:

tty=$(basename $(tty))
histdir=~/.hist
mkdir -p $histdir
export HISTFILE=$histdir/history.$tty
export PS1=$USER@'$PWD ['$tty'] ${?#0}$ '
exec /usr/bin/ksh

Save this as ~/bin/screenshell, and run chmod +x ~/bin/screenshell. Now you can run screen and have separate history files for each window.

SHELL=~/bin/screenshell screen

Examples:

Window 3

    vagrant@/home/vagrant [3] 127$ history
    1       history
    2       error
    3       history

Window 4

    vagrant@/home/vagrant [4] $ history
    1       history
    vagrant@/home/vagrant [4] $ echo $HISTFILE
    /home/vagrant/.hist/history.4
    vagrant@/home/vagrant [4] $ tty
    /dev/pts/4

An alternative would be to run exec ksh from .kshrc, but you'll need some kind of test to prevent an exec-loop.

2
  • Thank you for your code. I suspect it does not share the history between windows after session is closed, right?
    – Grzegorz
    May 31 '18 at 16:38
  • No, it doesn't. However, the number of history files is limited and it shouldn't be too much work to find and copy commands from other windows. Some answers to similar questions use $$ as selector, resulting in large numbers of files. May 31 '18 at 16:44

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.