Avoid storing duplicate entries in ~/.bash_history

I was curious if there’s a setting to keep ~/.bash_history from recording identical commands. So if you type the same thing many times in a row inside a console (Konsole in my case) it doesn’t add an entry each time, only once until you type something different. This can cause it to fill up pretty quickly and require more scrolling up to find an older command.

In addition, I was wondering if I could disable the console in Dolphin from creating pointless entries. I noticed that whenever I press F4 in the file browser to show its console I get random commands like “cd” and “clear” logged there except they start with a space.

Add the following to ~/.bash_profile:arrow_down:

export HISTCONTROL=ignoreboth:erasedups

However, that will not ignore duplicates which are separated by another command. It’ll only ignore successive invocations of the same command. And, it also allows for commands that start with a space to be excluded from the shell history.


Works just as desired now, thank you! Adding the following to ~/.bashrc gives full control of all the relevant settings:

# History settings
export HISTCONTROL=ignoreboth:erasedups
export HISTSIZE=1000
export HISTFILESIZE=100000

There is also HISTIGNORE to remove certain commands from history. I have it like this

export HISTIGNORE="xdotool*:ls"

to ignore spam of “xdotool” from an app I use to control the mouse from my phone, and ls because why the ■■■■ would I want ls in my bash history

1 Like

you can use:
history | grep
to search past commands, stop wasting time scrolling.

make an alias:
alias hg="history | grep"

then for example:
hg sudo
will show all the sudo you’ve done.
! then the number will execute

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.