[ksh] how to reload history file without entering a command

I'm basically looking for the ksh equivalent of bash's PROMPT_COMMAND="history -r" , where simply redrawing the command prompt in a terminal will cause ksh to reload the history file.

At the risk of sounding incredibly lazy (in which case I would be guilty as charged), I've noticed that if I have two terminals open--call them terminal A and terminal B--and run some commands in terminal A, I can only see those commands in terminal B (while scrolling up and down through the history) after I execute a command in terminal B. What I would like is for me to be able to just press enter at the command prompt in terminal B--without having to execute a command--and then be able to see the commands from terminal A.

Is there a setting in .kshrc that would force the shell to reload the history file each time the command prompt is drawn?

You might have better luck getting a reply from our ksh experts after the holidays.

Did you try searching the forums?

I did try searching, but no luck. I guess nobody using ksh as their interactive shell is as lazy as I am.

Hopefully one of the ksh experts will get back to me after holidays. In the meantime, I realized that ksh does not actually need to execute a command in order to reload the history file: While typing Enter by itself does not trigger a reload, typing anything (even just a semicolon) followed by Enter does.

So it seems that ksh's default behavior when it comes to the history file is "append then reload". My question for the experts is whether there is a command does "just reload". I've already looked at the ksh man page and snooped around for options to the fc command, no luck. If there is such a command, adding it to my prompt_commands function (see my "On korn shell, how to share history..." thread--apparently I'm still too new here to share the link) should do the trick.

Try:

alias r='fc -e -' 

in ksh 88. I'm not quite sure what you meant by refresh, history -r is "recall" the way we used it, anyway. Using r alias:
r somecommand # recall a command , to edit the command you see, you need to have previously exported the EDITOR variable export EDITOR=vi Use the up arrow to scroll in reverse time order (from newest to oldest) from the point the r alias finds for you.

# start recall from last command issued.
r 

Thank you, Jim, that is a useful alias but not what I'm looking for. I'll try to explain it differently:

When I'm working, I often have multiple terminal emulators open at once. If I pick any one of them and start pressing the up and down arrows, I don't see the commands recently run in the other open terminals. I'm looking for something to put in ~/.kshrc that will cause terminals to re-parse/reload my HISTFILE just by pressing Enter at a command prompt (without having to type anything at the prompt). This way, whenever I am jumping between terminals, when I pick a random one I can simply press Enter and then pressing the up and down arrows show me all the commands in my HISTFILE.

Hope that's more clear.

-- Post updated at 03:36 AM ---

Perhaps most succinctly: Is there a command that flushes korn shell's history buffer and replaces it with contents of HISTFILE?

Got it. Do not put a leading dot on the history file name, which means you have to declare HISTFILE. (ksh93). This lets you get buffering and then writing to the history file without erasing the contents. As an aside a lot of command C code evaluates environment variables. It may also set some.
Try:

strings/usr/bin/ksh | egrep '(HIST|FILE|SIZ)'

Then try dinking around with some of the variables. If the shell you use will not get updated like bash does, this can be one way to get custom behaviors.
Alternatively, if you can read C, consider spending some time read the source code for ksh93 if that is your shell. The code is now on github.
See: Building ksh from Source Code (Learning the Korn Shell, 2nd Edition)

1 Like

This is immensely important for technical people who are pushing the envelope or working on custom behaviors.

It's always best to go to the source code, when available, to understand what is going on under-the-hood and to add custom code.

Arguably, reading C or C++ code is a lot more difficult than reading PHP or Javascript or Python or C#, etc. at least for me, I find C or C++ is too down-in-the-weeds to easily grasp the functionality of the code.

I spend a lot of time coding, I mean a LOT Of time coding, and at least for me, writing code is a kind of digital artistic poetry, where bug hunting makes me smile and odd behavioral issues turn me into a kind of Sherlock Holmes for code.

Side story:

I was in truck riding along in the mountains with a US-born web developer turned real-estate agent recently. We were talking about coding and he told me he often slams the keyboard down or yells at his computer when coding and doing IT. He looked at me for affirmation of his frustration. I told in in reply that I mostly get joy from IT and coding, and often find myself smiling when bug hunting and when things do not go as expected. In fact, I told him that I find it hard to have any emotions when coding and problem solving, except for joy, and when I wake up in the morning, I am really excited to have that cup of java and login and code solutions my mind figured out when I was asleep.

Sometimes even here, I see people getting emotional over something (recently a moderator threatened to quit because they don't like some of my code changes and the transitions I am working on here). I am surprised by this and this has led to be believe I've been working in IT for so long, that writing code brings me truly great joy. Negative emotions are not really useful and so I don't have them over anything. However, I see a lot of IT people carry around with them a lot of frustration and anger and emotions related to IT and technology (mostly the middle aged and much older IT people, not the younger generation).

Anyway, I'm well off topic.

My point is that IT is a joy, coding is not some "geeky thing for losers" but it is a blessing and a great way to use your mind. Jim was right on to suggest looking at the ksh source for DevuanFan's custom ideas; and I hope DevuanFan will read the source and post back the code, the details he learned and his proposed solution

If you are in IT and do not love to code, search code, read code, write code, you are in the wrong business or hobby!

I am not really sure what you mean by that, but first let us be precise: are you talking about ksh88 (as it is found as the default shell in AIX and HP-UX) or ksh93?

Second, as far as i know there is no "history buffer" - apart from the history file - in ksh. You can have a separate history file for each shell instance (actually i like it that way because usually i need in a certain session the commands i used there before again, not the ones i used elsewhere, but that is a matter of personal taste) or you can have all sessions share a common history file. In each case "history" in the Korn shell is what is in this history file, nothing more, nothing less.

I hope this helps.

bakunin

Hi, bakunin. I'm finding that it is not so simple. If I have ksh running in a terminal emulator and manually edit the history file while the emulator is running, lines that I add to the HISTFILE will be picked up by the emulator. However, lines that I delete from the HISTFILE continue to show up in the terminal emulator's history. So it seems that the shell's idea of the history (which I'm calling its "history buffer") and what's actually in the HISTFILE are two different things. The only way for deleted lines to stop showing is to close the terminal emulator and open a new one.

Maybe my terminal emulator is introducing some unexpected behavior? I'll try a different emulator.

Thank you Jim and Neo. Yes, I'm finding that the only way to understand exactly what's going on will be to look at the source code. Alas, while my sh/bash, python, and perl kung fu is strong, C/C++ looks like gibberish to me, but I'll give it a shot anyway. If switching terminal emulators and/or recompiling ksh don't give me exactly the behavior I want, I may just go back to bash, which feels much more comfortable.

P.S. I'm using the public domain korn shell v5.2.14 (the default shell in OpenBSD 6.4).

Well, pdksh is famous for being barely compatible with any real ksh, so i'd like to put what i said above in perspective: it was meant about "real" ksh-versions, mainly about ksh88 as found in AIX and ksh93 as found in AIX and Linux. The (real, AST) Korn shell was made open source IIRC 2005, so there is IMHO no reason to work with rather inept look-alikes (or, rather, look-similars) like the pdksh at all. Drop it and get a real Korn shell from github or kornshell.com.

I hope this helps.

bakunin

1 Like

I recommend you open the C/C++ source code in a modern code editor, and having said that, I strongly recommend Visual Studio Code with the CPP extension:

C++ programming with Visual Studio Code

VSC also has extensions to prettify (format, beautify), indent, and to color matching brackets, braces and parens so all of this "cool stuff" makes code easier to read (and colorful, LOL)

Having the IntelliSense for CPP will help you understand the code. I write mostly in Javascript, PHP, CSS and HTML these days (nearly every day). VSC saves me a lot of time debugging syntax and keeping things moving along in a brisk development pace.

Thanks, bakunin. I'm new to the Korn shell, so had no idea about these nuances. I see that ksh93 is in the OpenBSD package repository, so I'll install it and see how it's different from pdksh .

Neo, I will give VSC/IntelliSense a try. If it makes CPP easier to read and work with, I will probably really like it.

In the meantime, I'm back to bash for when I want to work and not play/explore. I find that having this in my .bashrc gives me exactly the behavior I expect (namely, any time I press Enter in a terminal, the shell's idea of the history matches exactly what is in my HISTFILE--no more, no less):

PROMPT_COMMAND="history -a; history -c; history -r"

Thank you all very much. Happy hacking!

OK, i might be a bit slow but now i get what you want. If this is what you want you can have that in ksh too. There is no PROMPT_COMMAND variable in ksh but the shell prompt itself is capable of executing commands:

PS1='$(command; command; command; ...) <rest of your prompt here>'

Notice that editing the commad history needs the variable HISTEDIT (in ksh93) or FCEDIT (in ksh88) to be set to your preferred editor, otherwise you end up in ed . The history comamnd in ksh is hist , which by default is aliased to fc and r (for "repeat"). You can search and replace when repeating commands like this:

r old=new x

which will re-run the last command line starting with "x" but the instances of "old" replaced with "new".

I hope this helps.

bakunin

I had tried that, but apparently pdksh there is no history command: 'history' is just an alias for 'fc -l'

So you're saying the "real" ksh has 'history' command with these flags? Nice! I'm sorry about the ksh version mixup. It's tough being a noob!