Bash – Saving bash history from multiple Konsole not working correctly

bashcommand history

I use multiple Konsole terminals. And I want all the commands I type in every terminal to be saved in command history, so that next konsole i open will have all of them.
To prevent each terminal from over writing the other terminal's command history, I gave the following settings in my .bashrc

# avoid duplicates and commands starting with space
export HISTCONTROL=ignoredups:erasedups:ignorespace
# append history entries..
shopt -s histappend
#My machine reboots without warning sometimes.Hence to save commands instantaneously.
export PROMPT_COMMAND="history -a"  
export HISTSIZE=1000
PS1="\[\e[1;34m\]\! \[\e[0m\]"$PS1

I gave the last line to see the command number in my prompt.
The command no. has never gone above 600, but still some of my old commands are disappearing from the history. There are many commands which are given repeatedly, but as expected from ignoredups, it never increases the command no in prompt. Yet old commands are still disappearing.And the number of commands in history is always remaining slightly more than 500.

The .bash_history file still contains a lot of duplicates in spite of ignoredups.

PS: The output of echo $HISTSIZE and $HISTFILESIZE are both =1000


Update:
I found the problem in the above entry to .bashrc.
Just calling history -a in PROMPT_COMMAND simply appends the last new command to the .bash_history. So the ignoredups and erasedups have no effect.

Is there any way, I can still write to the .bash_history without duplicates from every terminal? I don't want to load the entire history at each command prompt by history -r and write it again back with history -w, because the commands I issued in one terminal will appear in another parallel running terminal also. I want the combined commands to appear only in a new terminal.

The puzzle, why my history was getting trimmed to 500 is solved. I noticed it happened each time I ssh into this machine. Creating a .bash_profile with the following entry solved this problem.

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

Now my .bashrc is executed each time I ssh too. And the history file size is now monotonically increasing.

Best Answer

I have concluded that it is not possible to append the new commands directly and at the same time to remove any duplicates. So my solution is to run the following command ones in a while. or to put it in my crontab.

tac $HOME/.bash_history | awk '!seen[$0]++' | tac > $HOME/.hist_Temp 
mv $HOME/.hist_Temp $HOME/.bash_history

The above command will keep the last occurrence of a command and remove every other repetitions above it from the history file without messing up the order.

Related Question