Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|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
    
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
add comment

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

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

1 Answer

up vote 0 down vote accepted

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.

share|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
    
@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
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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