1

I'd like to ignore all invocations of a command in my bash history, regardless of arguments. For example, to exclude all of ls, ls -l, and ls SOME_DIRECTORY, my understanding is that I'd need to do:

HISTIGNORE='ls:ls *'

I can't use just HISTIGNORE='ls*' because that also would ignore other commands that happen to start with ls, which is undesirable. However, needing to specify ls and ls * is annoying and error-prone as I add entries to HISTIGNORE.

Is there a pattern that I can use to match a command by itself and with arguments?

2
  • Perhaps I'm going about this the wrong way and instead should try to generate something like 'ls:ls *:cd:cd *' from ls:cd.
    – jamesdlin
    Feb 26 at 9:21
  • Depends on the usage. I would never influence my original history in this way, as the relationships between the lines would be lost. Instead I would create some tools (aliases, bash functions etc.) that would present the information I need. A custom ^R created with bind (see man bash) would help. Good luck.
    – JdeHaan
    Feb 26 at 9:34

1 Answer 1

1

HISTIGNORE pattern matching honors the setting of the extglob shell option and, with extended globbing, you can use this to match commands that start with ls and are followed by either a space and then anything else, or by nothing (end of the line).

shopt -s extglob
HISTIGNORE='ls@( *|)'

The @(foo|bar) format of bash extended globs will match exactly one of foo or bar. Here, the pattern we are giving is ls@( *|) so it will match ls at the beginning of the line and then either a space and 0 or more other following characters, or no characters at all.

Here it is in action:

$ shopt -s extglob 
$ export HISTIGNORE='ls@( *|)'
$ history -c
$ ls > /dev/null 
$ ls * > /dev/null
$ ls -la > /dev/null
$ ls file > /dev/null
$ lsa
-bash: lsa: command not found
$ history 
    1  lsa
    2  history 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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