I am trying to ignore some sensitive commands from being saved in history file in zsh.

HISTSIZE=1000                 # 1000 lines of history within the shell
SAVEHIST=1000                 # 1000 lines of history in $HISTFILE
HISTFILE=${HOME}/.zsh_history # Save history to ~/.zsh_history
## Ignore save in $HISTFILE, but still in the shell
HISTORY_IGNORE='([bf]g *|cd ..|l[a,l,s,h,] *|less *|vi[m,] *)'

But it still shows in history file:

% rm .zsh_history 
% ls              
Desktop  Documents  Downloads  Music  peda  Pictures  Public  Templates  Videos
% l
Desktop/  Documents/  Downloads/  Music/  peda/  Pictures/  Public/  Templates/  Videos/
% cat .zsh_history
cat .zsh_history

I have read the zshparam page but it doesn't help.

Is the part l[a,l,s,h,] * right?


l[a,l,s,h,] * does not match l because [] is not a comma-separated list of things to expand to - that's the syntax for brace expansion. With [...], [a,l,s,h,] is the same as [alsh,] and does not mean any of alsh or nothing. You need l[alsh]#.

For ls, the space seems to be breaking this match, even though everything after that is optional. So in practice, this seems to ignore ls only if it has an argument:

~ ls foo
ls: cannot access 'foo': No such file or directory
~ tail ~/.histfile
HISTORY_IGNORE='([bf]g *|cd ..|l[a,l,s,h,]*|less *|vi[m,] *)'
tail ~/.histfile
tail ~/.histfile

It seems you need to make the * optional as well:

HISTORY_IGNORE='([bf]g *|cd ..|l[alsh]#( *)#|less *|vim# *)'
  • Do you know where to find the full documentation about HISTORY_IGNORE?
    – mja
    Jan 28 '18 at 3:15
  • 1
    @MarkJ.Adams zshparam is about it. That said, the manpage mentions [[ $1 != ${~HISTORY_IGNORE} ]] in the function on removing entries from history, and I think that test matches up pretty well with the actual behaviour of HISTORY_IGNORE on command lines
    – muru
    Jan 28 '18 at 3:18

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.