I hate the bash ‘history’ command. It never finds the history items I want. Sometimes I can get what I want with ‘history | grep XXX’ but often either the history is too long (400+ hits! yay) or too short (no hits. Boo).
I thought to myself, why dont I write a little ‘history helper’ command that lets me basically ‘star’ specific history items and make them always turn up in my history, with both a global star list and a cwd specific star list.
Sounds amazing. So I naively tried to read the history by doing this:
system("history > blah")
FILE *fp = fopen("blah", "r")
Oh, that didn’t work. Ah, I get it, the system() command runs in it’s own context and can’t access the bash history. Drats. Ok, so I’ll try reading ~/.bash_history
ba baaa~ Nope. That’s only got the history up until the last call to either a) history -w, or b) call to exit from the shell.
So, question: How can I programatically access the current bash history in C?
(NB. No, not what’s in ~/.bash_history, the current history, the exact output you see when you type ‘history’ from the prompt; also, why C you ask? why not…but really, because I already have a handy ncurses wrapper I was going to use to let me get fancy auto-completion and sub-selection history searching on normal terminals…)
Edit: Please, if you have something useful to contribute, I’m happy to hear it, but I get a little sick of comments like ‘you should read the bash history man pages’.
I have read them. There is nothing that allows me to ‘star’ a history item and have it always turn up in my history, and nothing that allows me to maintain a per-directory history context. These are things that history does not support. I appreciate this. That’s why I want to write a helper function, for my specific purpose. If you have a comment like “maybe you should read the history man pages some more”, please just hold it in, and don’t respond; it doesn’t really address my question. 🙂
The closest you can get is
and read standard input from the application.