bash security zsh command-history – How to Keep a Command from Being Added to Your History?

bashcommand historySecurityzsh

Is there any way to keep a command from being added to your history? I have a command that I want to keep out of my history file, and I really don't care to have it there when I search the history stored in memory, though that's less of a concern. Is there any way to prevent this, or do I just have to go back and edit my history file.

update: I didn't realize this might be shell-specific. My shell is zsh. You're welcome to answer for other shells so people know how to do this in their shell.

Best Answer

In ZSH:

First insert setopt HIST_IGNORE_SPACE to your ~/.zshrc. Now after you log in again, you can prefix any commands you don't want stored in the history with a space. Note that (unlike bash's option of the same name) the command lingers in the internal history until the next command is entered before it vanishes, allowing you to briefly reuse or edit the line.

From the user manual, the following 3 options can be used to say that certain lines shouldn't go into the history at all:

  • HIST_IGNORE_SPACE don't store commands prefixed with a space
  • HIST_NO_STORE don't store history (fc -l) command
  • HIST_NO_FUNCTIONS don't store function definitions