I’m writing my PROMPT_COMMAND="history -a;$PROMPT_COMMAND" but I still get some duplicates from different terminal sessions. It seems I still will get some duplicates but I get less using the command PROMPT_COMMAND="history -w;$PROMPT_COMMAND".
I know I can do the history -a;history -c; history -r but I don’t want them all synced. I only want that to occur when I call history -n. I’m basically down to using either history -a or history -w but I can’t seem to find the difference between the two. Which one would be better to have to avoid as many duplicates as possible.
history -awill append your current session history to the end of the history file.history -wwill replace the content of the history file with your current session history.Which one avoids more duplicates? Theoretically neither. Neither
-anor-wchecks for duplicates.One can argue that
-wavoids more duplicates but only because it removes the previous content of the history file. So any potential duplicate entries in the file are eliminated. Along with anything else in the file.Background information:
In any bash session there are at least two versions of history. One is the history that is stored in the memory of the currently running bash process (the session history). The other is the history saved on disk (the history file).
Each running bash session has its own session history. Typically there is only on history file shared by all bash sessions.
When working in bash you typically only work with the session history. the history file is only touched on certain events. some of the events are: bash startup, bash shutdown, invocation of
history -aorhistory -w. There are more but that is beyond the scope of this question.Typically bash is configured as follows: on bash startup read from history file to session history. while running only manipulate the session history. on shutdown replace content of history file with current session history.
Bash has features to avoid duplicates in the session history. But as far as i know no features to avoid duplicates in the history file.
Bash features to avoid duplicates in the session history (not the history file):
set the special bash variable
HISTCONTROLtoignoredups. now if you execute the same command twice (immediately one after the other) the second will not be saved to session history. it does not remove previous entries which are not immediate.set the special bash variable
HISTCONTROLtoerasedups. now if you execute a command it will remove all previous instances of that command in the session history. but note it only does so in the current session history. It does not search for duplicates in the history file. It will also not prevent duplicates from entering your session history when usinghistory -n(read entries from history file to session history).more details in the bash manual:
about the
historycommand.about the special variable
HISTCONTROL.see also
https://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows#
https://serverfault.com/questions/337123/strange-bash-history-behaviour-when-running-multiple-sessions