Hello all,
I've got a problem with running the "history" command in a sourced (preloaded) function:
[snip]
R.Wieser wrote:
Hello all,
I've got a problem with running the "history" command in a sourced
(preloaded) function:
[snip]
Hi Rudy,
I think we'll need to know which shell you're using, and which version, in order to give you a good answer.
If you're using Bash--what is $HISTCONTROL set to?
If you're using Bash-
-what is $HISTCONTROL set to?
To more things along......
bash-5.2$ . ./x
bash-5.2$ history
1 foobar
2 history
bash-5.2$ histtest
bash-5.2$ history
1 history
I've got a problem with running the "history" command in a sourced (preloaded) function:
- - - - - - - - - -
histtest() {
history -c
history -s "foobar"
}
histtest
- - - - - - - - - -
When I source this script the history gets cleared and the "foobar" entry is added to it. IOW, it works as expected.
However, when I than afterwards just type "histtest" on the commandline the history is cleared, but the "foobar" entry isn't added.
Question: what causes the difference and how do I fix it ?
Remark: the trouble seems to come from clearing the history. When I remove that line the "foobar" entry is, in both cases, added as expected.
I'm just guessing but I think it might be related to _when_ the clearing
is actually done
$ echo zzz ; history -c ; history -s foo
zzz
$ history
1 history
probably is not,
unless clearing history is by design performed after the whole line is interpreted/executed.
I don't know if it can be fixed (or if there's anything to be fixed)
but since sourcing works as expected, why not just utilize it?[snip]
Hello all,
I've got a problem with running the "history" command in a sourced (preloaded) function:
- - - - - - - - - -
histtest() {
history -c
history -s "foobar"
}
histtest
- - - - - - - - - -
When I source this script the history gets cleared and the "foobar" entry
is added to it. IOW, it works as expected.
However, when I than afterwards just type "histtest" on the commandline
the history is cleared, but the "foobar" entry isn't added.
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.
I did find a solution that seems to work with Bash 5.2.15(1)-release:
histtest() {
history -s foobar
history -d 1--2
}
- - - - - - - - - -
histtest() {
history -d 1--2
history -s "foobar"
history -s "barfoo"
history -s "more stuff"
}
histtest
- - - - - - - - - -
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"!
Update :
I just tried the below and it works the same way, showing just the three
"-s" lines after the "-c", either by sourcing it or (afterwards) just executing the function :
- - - - - - - - - -
histtest() {
history -s dummy
history -c
history -s foobar
history -s barfoo
history -s more stuff
}
histtest
- - - - - - - - - -
FWI : I started with "history 1--2", but that throws an error when the history is empty - otherwise an "out of range" error is thrown and nothing
is added.
I'm back to being baffled, both in regard to the result as well as what
might be causing it.
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.
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.
This is very odd.
Just having one -s before the -c makes things work as expected (for
me):
but commenting out that "history -s dummy" line and we are back to
the original odd behaviour.
I don't know how complicated your real version of histtest is,
but you might consider creating an array of items that should be
added to the history.
My point is that the user *did* type in "histtest2" and yet that
command does not appear in the history.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 135:14:05 |
| Calls: | 12,087 |
| Files: | 14,997 |
| Messages: | 6,517,362 |