I am having a lot of trouble setting up the terminal history of Bash the way I want.

I would like to have no duplicate entries and if I enter a command I want it saved and the duplicates above removed.

The problem is the history command shows me it is functioning the way I want however once I log out the duplicates come back again. I believe it is just appending the history to the existing one.

I have these lines in my .bashrc file (~/.bashrc)

HISTCONTROL=ignoreboth:erasedups
shopt -u histappend

I have even tried uncommenting shopt but it still appends the history on logout.

How can I have the history be exactly how it is before I logout?

Solution: You can create a .bash_logout file (~/.bash_logout) with the following content:

history -a
history -w

This writes the current history that is in memory to the .bash_history file and then removes duplicates before you logout.

link|improve this question
Ah didn't know of it's existence, cheers. – Craig Sep 15 '11 at 3:43
Did you try history -w? It should write the history before logout. – ztank1013 Sep 15 '11 at 5:42
@ztank1013 thanks that lead me in the right direction, I have now added the solution. – Craig Sep 15 '11 at 7:54
feedback

migrated from stackoverflow.com Sep 15 '11 at 6:05

This question came from our site for professional and enthusiast programmers.

1 Answer

You can create a .bash_logout file (~/.bash_logout) with the following content:

history -a
history -w

This writes the current history that is in memory to the .bash_history file and then removes duplicates before you logout.

(Adding this as a solution so it is no longer a zero-answer question)

link|improve this answer
Bash normally automatically saves the history. What's special about this sequence of history commands? And why does writing after appending make a difference? – Chris Page Sep 26 '11 at 12:33
feedback

Your Answer

 
or
required, but never shown

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