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)

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?

  • 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
  • You should add the solution as an answer and accept that yourself, so the question will be marked as solved. – Daniel Andersson Jun 1 '12 at 7:42

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.

  • 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
  • @ChrisPage It's hard to explain in the limited text here but basically there are three histories. session history - everything newly entered in the session, history file - past history of everything before login and current history - what you see when you list the history (combination of the history file and session history). – Craig Jun 10 '12 at 8:06
  • @ChrisPage when you use erasedups it stops you having duplicates in the session history only. If for example you had the command ls in the history file and you used it in this session current history would only show one entry for the command ls. However by default Bash appends session history to the history file both of which contain the command ls so next time you log in there are 2 ls commands listed. If you use it again there will be 3 next time you log in. – Craig Jun 10 '12 at 8:13
  • @ChrisPage by running history -a really what it does is the same as the default save that Bash does however this is not why we use it here. We use it because it removes all duplicates in the current history so that current history is now exactly what we want the history to look like. we then use history -w to write the current history to the history file. Hope that all made sense. I know by looking at it is seems silly to use history -a when you are going to overwrite it again with history -w but the key is history -a changes the current history. – Craig Jun 10 '12 at 8:20
  • @Craig Thanks, that’s helpful, but is it actually necessary to history -w? Won’t bash save it automatically? – Chris Page Jun 14 '12 at 20:15

@Danny Staple's answer is good, but doesn't work for me, once I logged out the duplicates come back again (I'm using Ubuntu 20.04 BASH). But that give me a right direction to go. I fine tune it a little. Likewise, go editing the file ~/.bash_logout as following:

history -w
history -c

Finally, history duplication won't bother me any more.

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.