Problem
I want to append an item to history from a function, but don’t want to run it.
So from inside the function I call history -s
.
This works, but causes the function call itself to not be added to the history file.
Example:
$ test123() { history -s 'echo "here"'; }
$ test123 "args"
$ cat "$HISTFILE"
...
echo "here"
If I remove history -s
from my function, and call the function again, the function call is correctly added to the History File:
$ test123() { echo "nothing"; }
$ test123 "args"
$ cat "$HISTFILE"
...
test123 "args"
What I have Tried:
I have tried adding to my function history -n
and history -w
to load history file to current history lines and to write to the history file. But this doesn’t solve the problem.
In my ~/.bashrc
to preserve history between terminals and append automatically to history file after a command is run I have:
shopt -s histappend
export PROMPT_COMMAND='history -n; history -a'
This shouldn’t be causing my problem, but I still tried changing and removing these values and it is still the same result.