history -a vs -w in BASH. How to replace/append and ignore/erase duplicates?
You stated two questions:
What is the difference between
history -a
andhistory -w
?
history -a
will append your current session history to the content of the history file.
history -w
will replace the content of the history file with your current session history.
Which one avoids more duplicates?
Theoretically neither. Neither -a
nor -w
checks for duplicates. Practically -w
avoids more duplicates because it replaces the content of the history file. Any potential duplicate entries in the file are eliminated, along with anything else in the file.
Read more about the history
command in the bash manual.
Some more information about other possibilities to kill duplicates:
The special bash variable HISTCONTROL=ignoredups
doesn't help much here because that only eliminates duplicates as they are entered in the running shell. It does nothing to prevent existing duplicates in the current history or in the history file. Furthermore ignoredups
only ignores commands that is a duplicate of the previous command, not if it is a duplicate of any command previously in history.
Unfortunately not even the much promising HISTCONTROL=erasedups
helps much here. erasedups
erases any history entry matching the currently entered command. Sadly, 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 using history -n
.
Read more about the special variable HISTCONTROL
in the bash manual.
J Spen
Updated on June 26, 2022Comments
-
J Spen about 2 years
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 commandPROMPT_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. -
J Spen almost 13 yearsThanks that explained it. I'll stick with history -a then. I just wrote a python script to delete duplicates in history and only keep the most recent so I can just run that occasionally if necessary. Cheers
-
J Spen almost 13 yearsthanks but seen that article mainly needed to know the difference between -w and -a.
-
reader_1000 almost 13 yearssorry for giving irrelevant information. I misunderstood your question.
-
J Spen almost 13 yearsNo worries, it was kind of confusing.
-
orome over 4 yearsThere seems to be no difference between
-a
and -w` on macOS: both append, as near as I can tell. Am I missing something?