Benjamin,
> The Bash manual seems misleading about how the "history" command works.
> It claims that the -c option "may be combined with the other options to
> replace the history list completely," but if you try the obvious thing,
>
> history -c -s foobar
>
> the history is simply cleared and no "foobar" entry is present afterward.
There is a (good) possibility that its not the history command itself thats
to blame, but the underlaying bash (or OS).
> I did find a solution that seems to work with Bash 5.2.15(1)-release:
>
> histtest() {
> history -s foobar
> history -d 1--2
> }
I was also thinking of that (did first try to replace the "-c" with a
full-range "-d" to see if it would make a difference), but didn't really
want to go for "circumvent the problem" hacks. Besides having to keep track
of how many "history -s" additions I would be doing to be able to remove the
rest.
In that regard I already I already found a "solution" to the problem by
echo-ing the "history -s" parts into a file, and than load that file after I
cleared the history. It works well enough, but I don't like it.
<following a brainfart and doing some checking>
Funny : that "history -d 1--2" causes a quite unexpected behaviour :
> - - - - - - - - - -
> histtest() {
> history -d 1--2
> history -s "foobar"
> history -s "barfoo"
> history -s "more stuff"
> }
> histtest
> - - - - - - - - - -
Sourcing this script causes the history to end up with four lines, the
oldest one being the command sourcing the script.
However, if I than afterwards execute "histstest" I get just the
"history -s" lines. <whut? why don't they disappear now ?>
If I would be a fan of hacks that won't break down when the underlaying
problem is fixed than I would probably use this one. :-)
Hmmm... Somehow I get the feeling that some lazy memory allocation (new
storage for the history) is involved.
> I noticed that if the function only runs "history -s foobar", and does
> not delete any history entries, the *invocation* of the function is
> nevertheless excluded from the history listing. In other words, if you
> run the three commands
>
> histtest2() { history -s foobar2; }
> histtest2
> history
>
> then the history listing will include "foobar2" but it will not show
> that you ran "histtest2"!
IIRC, that is defined behaviour : only commands started from the commandline
(typed by the user!) are remembered in the history.
And I don't think you really want to have it remember everything - just take
a peek at the output of "declare -F" (or "declare -f" if you want to see the
contentst too) to see how may scripts bash has predefined. Imagine yourself
having to wade thru a sh*tload of bash commands that you can't remember
having ever typed (because you didn't) ...
Regards,
Rudy Wieser