3

I have the following in my ~/.bashrc

HISTCONTROL=ignoreboth:erasedups

However my history contains many duplicates e.g. I have 5 entries for sudo apt-get upgrade.

I am running Raspbian Jessie and principally access via ssh.

PS I just entered sudo apt-get upgrade while writing this and now only have a single entry. Any idea why, and how I can prevent it?

  • From the way you phrased your question, it is unclear if the duplicates were recorded before of after adding HISTCONTROL setting. – techraf Mar 5 '16 at 1:09
  • @techraf I have had this setting for some years (migrated from OS X), this is not a new problem. I was just finding it annoying when reviewing my history. – Milliways Mar 5 '16 at 2:38
4

No need of erasedups as you are already giving ignoreboth.

Ignoreboth will take care of duplicates and white space commands.

Just try only ignoreboth or erase dups.

"HISTCONTROL=ignoreboth"

Use only this and reload bash . Thank you.

  • From what I understand reading the bash man page, "ignoredups" and "erasedups" are slightly different. "ignoredups" causes a line matching the previous entry not to be saved. "erasedups" causes all previous matching history entries to be erased (not just the previous one). – mtklr Mar 6 '16 at 3:00
  • I changed to HISTCONTROL=ignorespace:erasedups which seems to work and do what I want i.e. put latest entries last and replace older entries. – Milliways Mar 7 '16 at 0:24
  • No. A erasedups will scan the whole history and remove all found dups. The ignoredups will ignore only the last dup. They are not equivalent. Only if the list of commands is clean of dups both will produce similar results. But erasedups imply more work as the whole list will be scanned. – Isaac Jul 1 '18 at 10:31
0

To uniquely record every new command is tricky. First you need to add to ~/.profile or similar:

HISTCONTROL=erasedups
PROMPT_COMMAND='history -w'

Then you need to add to ~/.bash_logout:

history -a
history -w
  • No. Every new command is recorded in memory (as configured), history command will list such recorded commands list. To send that list of commands to a file you need history -w. The erasedups acts on the memory list, not on the file list. – Isaac Jul 1 '18 at 6:27
-1

There are two lists of commands:

  • In memory.
  • In file.

The setting for HISTCONTROL acts over the memory list. That list could be seen by the command history without options. If you type a command it will be appended to the list. If the command to add is equal to the last command in the list and the option ignoredups is set, or the option erasedups is set, the command will be discarded. In this case, both options have some similarity.
If the option erasedups is set, all previous appearances of the command will be removed from the memory list (but not from file). This removal will only happen for a new repeated command. That is: repeated commands will stay until a new command is to be appended to the list.

To write the memory list to file there is the command history -w (which will replace the whole file by a new copy). The list in file will not be updated until it is written. Or appended with history -a (which will append only new lines that have not been appended before and may leave older repeated commands in the file). When bash exit, the option histappend will choose between replacing or appending to the file. Apending may leave repeated commands intact.

One way to clean the file list (keeping the last appearance of a command) is:

$ history | sort -urk2 | sort -n | sed 's/^ \+[0-9]\+ \+//'  >~/.bash_history

One way to append each command to file is to use the prompt command bash variable to execute a history -w on each new command line:

$ PROMPT_COMMAND='history -w'

To make this automatic, all the following commands should be added to ~/.bashrc (usually in linux) or ~/.profile (usually in mac):

HISTFILE=${HISTFILE:-~/.bash_history}
HISTCONTROL=ignoreboth:erasedups
PROMPT_COMMAND='history -w'
nl "$HISTFILE"|sort -k2 -k 1,1nr|uniq -f1|sort -n|cut -f2- >"${HISTFILE}.temp" && mv "$HISTFILE"{.temp,}
history -c; history -r

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.