Home > Linux How to, Pro Linux > Avoid duplicates in bash history | bash history duplicates

Avoid duplicates in bash history | bash history duplicates

September 27th, 2011 Leave a comment Go to comments

bash logo small 150x150 Avoid duplicates in bash history Sometimes when working, specially when writing and testing scripts, I have the need to work in the $SHELL and repeat the same commands over and over as a matter of fact if I check my $HISTORY for a command I find something like similar to :

  592  vim script.sh
593  chmod +x script.sh
594  ./script.sh
595  vim script.sh
596  ./script.sh
597  vim script.sh
598  ./script.sh
599  vim script.sh
600  ./script.sh
601  vim script.sh
602  ./script.sh
603  vim script.sh
604  ./script.sh
605  vim script.sh
606  ./script.sh
607  vim script.sh
608  ./script.sh
609  vim script.sh
610  ./script.sh
611  ./script.sh

I guess you got an idea as this comes from my work computer as I’m deep in some serious scripting these days, not very handy nor very elegant either as everytime I need to search for a command I used a while back I’m forced to grep through the results or use CTRL + r as I cannot force myself to use “!!” or something history command itself.

While looking for a solution for bash history duplicates  I came came across a section of the bash man page that described exactly what I was looking for.

Add the following line to your .bashrc file so that the $SHELL will not keep in history duplicates of the same command as I did above :

 # User specific aliases and functions
export HISTCONTROL=erasedups:ignoredups 

So what this line does? Well ignoredups simply tells bash history to ignore duplicates, hence making $HISTORY “nicer”, while erasedups causes all previous lines matching the current line  to  be  removed from the history list before that line is saved. Actually there are many other switches and options that can be used but this is what I needed for my modest needs (even because 99% of the time I’m not working locally on my system icon smile Avoid duplicates in bash history )

Here’s en except from the bash man page describing the various switches you can use in your .bashrc file to configure the shell to your needs.

 HISTCONTROL
A colon-separated list of values controlling  how  commands  are
saved  on  the  history  list.   If  the list of values includes
ignorespace, lines which begin with a space  character  are  not
saved  in  the history list.  A value of ignoredups causes lines
matching the previous history entry to not be saved.  A value of
ignoreboth is shorthand for ignorespace and ignoredups.  A value
of erasedups causes all previous lines matching the current line
to  be  removed from the history list before that line is saved.
Any value not in the above list is ignored.  If  HISTCONTROL  is
unset,  or does not include a valid value, all lines read by the
shell parser are saved on the history list, subject to the value
of  HISTIGNORE.  The second and subsequent lines of a multi-line
compound command are not tested, and are added  to  the  history
regardless of the value of HISTCONTROL.

Well I hope you will find this little trick useful, let me know your thoughts or if you have any question.

Cheers Lethe.

VN:F [1.9.17_1161]
Rating: 0.0/5 (0 votes cast)

Incoming search terms:

Categories: Linux How to, Pro Linux