You are not logged in.

#1 2012-10-04 05:57:28

amadar
Banned
Registered: 2011-04-15
Posts: 147

[SOLVED] Bash history doesn't save properly. Why not?

I've set HISTCONTROL=erasedups in /etc/bash.bashrc and the history appears to work as intended during the session, but after I exit and start a new Bash session, the history is as if I had never set the HISTCONTROL variable in the first place. For example, duplicates are not erased like I thought they would be.

For example, now that I have set HISTCONTROL=erasedups, running ls will erase all previous entries of ls in the history during my session and when I run history, I will only see ls as the second to last thing after history in the list. There will are no previous entries of ls seen in the history list because of HISTCONTROL=erasedups. However, when I exit the shell and start a new one then view the history, all the previous entries of ls are still there again, including that last one I did in the last session. So basically, the erasedups feature works in the session, but the erasedups changes aren't propagated to the file designated by HISTFILE. New commands are written to the HISTFILE though.

Any idea why history entries are only erased in the session (memory) and not the history file?

Last edited by amadar (2012-11-19 13:17:08)

Offline

#2 2012-10-04 06:18:57

DSpider
Member
From: Romania
Registered: 2009-08-23
Posts: 2,273

Re: [SOLVED] Bash history doesn't save properly. Why not?

You can only use one terminal at a time. If you're running guake or tilda in the background, and you're using some other terminal for commands, the commands that you're inputting in this other terminal will not be saved in the ~/.bash_history file.

What is this about multiple sessions? Do you have multiple users for your machine? Then each would have their own ~/.bash_history in their respective user dirs.

Last edited by DSpider (2012-10-04 06:19:53)


"How to Succeed with Linux"

I have made a personal commitment not to reply in topics that start with a lowercase letter. Proper grammar and punctuation is a sign of respect, and if you do not show any, you will NOT receive any help (at least not from me).

Offline

#3 2012-10-04 07:07:37

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,019

Re: [SOLVED] Bash history doesn't save properly. Why not?

Just a thought:
  'ls /mnt' and 'ls /media' are not duplicates. The bash history works on lines so the whole line has to match. I don't know if that was the cause of the issue but thought I'd mention it.
I didn't read anything about multiple sessions. OP said a session was ended and in a new one the changes to history weren't there.


You're just jealous because the voices only talk to me.

Offline

#4 2012-10-04 09:18:33

amadar
Banned
Registered: 2011-04-15
Posts: 147

Re: [SOLVED] Bash history doesn't save properly. Why not?

@DSpider I tested it and yes, multiple terminals save their commands to the history. However, having only one single terminal open (no tty, just one Gnome Terminal) then exiting after running commands still doesn't apply the erasedups state to the file. The erasedups functionality is only visible in the session. When I exit and come back (one single terminal) then the entries that were supposedly erased are not erased, but the new entries are there.

Offline

#5 2012-10-06 06:00:57

thisoldman
Member
From: Pittsburgh
Registered: 2009-04-25
Posts: 1,172

Re: [SOLVED] Bash history doesn't save properly. Why not?

The Bash variable HISTCONTROL will only control duplicates for the current session.  It doesn't check the history file for any previous duplicates.  Considering that a history file has no upper limit on size or number of lines except what a user sets, this is probably a good behavior.  You can add the history file to the current session's command list using 'history -n' or 'history -r'.  See 'help history' for the short listing of options or wade into 'info bash' and remember to come up for air after you fall.

If you want to eliminate duplicate commands in your history file and keep the commands in the same order as they originally appear, I think the following should work.  I'm not confident of my awk skills so this should be well-tested before adding it to your ~/.bash_logout.

$ awk '!x[$0]++'  .bash_history > .bash_history.tmp && mv .bash_history.tmp .bash_history

If you don't care about command order, some variation of 'sort -u' will work to remove duplicates.

Edit – forgot the initial '.' on the filenames.

Last edited by thisoldman (2012-10-06 06:04:45)

Offline

#6 2012-11-19 13:16:37

amadar
Banned
Registered: 2011-04-15
Posts: 147

Re: [SOLVED] Bash history doesn't save properly. Why not?

I ended coming up with something similar to that based on this excellent answer I found on stackoverflow: http://stackoverflow.com/questions/3382 … er-7449399

Offline

#7 2012-11-19 14:54:16

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,697
Website

Re: [SOLVED] Bash history doesn't save properly. Why not?

FWIW, I don't modify the history file, but I had similar goals in displaying results so I wrote an hgrep function:

hgrep() {
    history > /tmp/hist
    s="${@/\//\\/}"
    awk '/'"$s"'/ {$1=""; print}' /tmp/hist | sort -u | while read line; do
        grep -x -m 1 "$line" /tmp/hist
    done | sort -u
}

I put this is my bashrc so I can use "hgrep <some part of a line>" to see unique previous uses.

EDIT: added '-x' flag which resolves some 'corner cases'

Last edited by Trilby (2012-11-23 12:31:01)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#8 2012-11-23 12:25:36

amadar
Banned
Registered: 2011-04-15
Posts: 147

Re: [SOLVED] Bash history doesn't save properly. Why not?

That's a nice idea for filtering the history.

Offline

Board footer

Powered by FluxBB