37

I was reading the manpages for bash on the plane and I stumbled across the HISTIGNORE variable. To test it out, I immediately edited my .profile to define this variable:

export HISTIGNORE=ls

I tested it out and this is great! It excludes plain 'ls' commands from my history without excluding more interesting commands with lengthy paths, but having recently harvested a great deal of wisdom from SU I am eager to know what other commands superusers might recommend or other lessons learned.

What can you share about using the HISTIGNORE variable in bash?

2 Answers 2

38

I ignore ls without commands, bg, fg, exit, pwd, clear, mount and umount:

If you include this in your HISTIGNORE string you can suppress anything as you wish by adding a space at the front of the command:

"[ \t]*" 

This is my HISTIGNORE:

HISTIGNORE="&:ls:[bf]g:exit:pwd:clear:mount:umount:[ \t]*"

I've excluded some other stuff that I have in there that are repetitive commands that are unique to my server. Anything you do that is simple is a good thing to exclude.

I have other stuff which I have forgotten to add but I'm miles away from my Linux box so I am going off of memory.

5
  • 9
    If the value of HISTCONTROL includes "ignorespace" or "ignoreboth", commands that begin with a space are omitted from history. If the value includes "ignore dups" or "ignoreboth", it's the same as "&" in HISTIGNORE. Jan 14, 2011 at 23:03
  • To me it has also proven useful to histignore the history command itself.
    – con-f-use
    Apr 17, 2013 at 21:07
  • 2
    using "[ \t]*" ignores command starting with a space or t. You want HISTIGNORE=$'&:ls:[bf]g:exit:pwd:clear:mount:umount:[ \t]*' Oct 22, 2015 at 19:11
  • 1
    Well, ignoring commands that started with a space or \t was the intention. What is that $ at the front doing there, that looks very weird, I wouldn't do it like that.
    – daparic
    Mar 27, 2021 at 11:38
  • 1
    mine is export HISTIGNORE="history:forget*" with an alias of alias forget="" . it makes not adding something to your history a little more explicit than a single space. Jun 8, 2022 at 1:50
21

Don't save trivial one and two character commands on the history list:

HISTIGNORE='?:??'

If the extglob option is enabled, you can also use extended patterns, e.g.

HISTIGNORE='a*( )'
2
  • Am not sure about your ??, but sometimes I do od /path/of/important/file and I need to remember this.
    – daparic
    Mar 27, 2021 at 11:40
  • * works as an arbitrary length placeholder as well. So for example for my custom history search shortcut, I use h *, so that these searches don't show up in the history itself again. Apr 13, 2021 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.