Linux / FreeBSD: Avoid bash history duplicate lines with HISTCONTROL
Linux / FreeBSD: Avoid bash history duplicate lines with HISTCONTROLPlease see previous article "How to increase bash history size in Linux" [url]/how-to-increase-bash-history-size-in-linux-t19491.html[/url] first.
Bash history, by default, is 500 lines long. Running the same command multiple times, consume multiple bash history entries. Example:
Code:
[user@linux ~]# one-command
-bash: one-command: command not found
[user@linux ~]# one-command
-bash: one-command: command not found
[user@linux ~]# one-command
-bash: one-command: command not found
[user@linux ~]# one-command
-bash: one-command: command not found
[user@linux ~]# one-command
-bash: one-command: command not found
[user@linux ~]# history | tail
504 one-command
505 one-command
506 one-command
507 one-command
508 one-command
509 history | tail
This is also inconvenient when searching through history (using CTRL+R) or shifting with up/down keys and the same command pops up every time.
To avoid duplicate lines in bash history use the following bash variable in /etc/profile.d/bash_hist.sh file:Code:
export HISTCONTROL=ignoreboth:erasedups
And the result:
Code:
[user@linux ~]# two-command
-bash: two-command: command not found
[user@linux ~]# two-command
-bash: two-command: command not found
[user@linux ~]# two-command
-bash: two-command: command not found
[user@linux ~]# two-command
-bash: two-command: command not found
[user@linux ~]# history | tail
503 one-command
504 one-command
505 one-command
506 one-command
507 one-command
508 export HISTCONTROL=ignoreboth:erasedups
509 two-command
510 history | tail