HISTCONTROL=ignoreboth doesn't work

8,197

It's working as intended. ignoredups, implied by ignoreboth, doesn't add a command to history if it's the same as the immediate previous command. It doesn't look further back in history. From the manual:

A value of ‘ignoredups’ causes lines which match the previous history entry to not be saved.

So, this will add foo twice to the history:

$ foo
$ bar
$ foo

But so will this:

$ foo
$ bar
$ foo
$ foo

The last foo won't be added to history, since the previous command was foo. Use erasedups in conjunction:

A value of ‘erasedups’ causes all previous lines matching the current line to be removed from the history list before that line is saved.

So:

HISTCONTROL=ignoreboth:erasedups
Share:
8,197

Related videos on Youtube

Old Geezer
Author by

Old Geezer

Don't shoot the messenger. An expert, or teacher, is a person who, after reading your question, knows what you know, what you don't know, what you are trying to know, and what else you need to know in order to achieve what you are trying to know.

Updated on September 18, 2022

Comments

  • Old Geezer
    Old Geezer over 1 year

    The default .bashrc in the standard distribution of Ubuntu 16.04 that comes with AWS has these lines:

    # don't put duplicate lines or lines starting with space in the history.
    # See bash(1) for more options
    HISTCONTROL=ignoreboth
    

    However, it doesn't seem to work. I have been running pm2 restart myApp and pm2 list repeatedly using the Up Arrow key, and the command buffer now contains nearly a hundred lines of these.

    What could be wrong?

  • Jerry Green
    Jerry Green over 2 years
    That is the problem: it doesn't work. I run foo 3 times in a row, exit bash, lookup history, - and it contains 3 foo! I am using zsh 5.8 (x86_64-apple-darwin20.0) / GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20) (default on macOS Big Sur)