Pergunta

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.

Foi útil?

Solução

You stated two questions:

What is the difference between history -a and history -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.

Outras dicas

you can avoid duplicates by using this

export HISTCONTROL=ignoredups

You can look at this for more information:

http://stefaanlippens.net/bashduplicates

Use BASH variable PROMPT_COMMAND with history and using sort and uniq commands:

$> export PROMPT_COMMAND="history -a && history -c && sort ~/.bash_history | uniq > ~/.bash_history_new && rm ~/.bash_history && mv ~/.bash_history_new ~/.bash_history && history -r"

With this you don't need to set shopt -s histappend and do export HISTCONTROL=erasedups.

This will keep your history and history file without duplicates on-the-fly. You can play with keeping history in order commands has been typed (hint: use line numbers in history, sort from specific line start position and after removing duplicates do sort again with line numbers).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top