94

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.

tshepang
  • 65,642
xenoterracide
  • 59,188
  • 74
  • 187
  • 252
  • 1
    cross-linking to zsh answer: http://superuser.com/questions/352788/how-to-prevent-a-command-in-the-zshell-from-being-saved-into-history – Michael Shigorin Oct 26 '15 at 10:52

4 Answers4

85

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
Peter Cordes
  • 6,466
dogbane
  • 29,677
  • any chance @mmckinst answer works in zsh? or something like it? or perhaps any command that includes the command | somecommand – xenoterracide Jan 19 '11 at 10:57
  • AFAIK there is no equivalent of bash's HISTIGNORE in zsh. – dogbane Jan 19 '11 at 11:01
  • @xeno zsh provides a hook function preexec that can be used for that. If it returns non-zero the command is not saved in history. – Keith Jan 19 '11 at 14:20
  • 1
    in order to have zsh ignore a bunch of basic common commands, I aliased them all to have a space in front...

    for c (ls fg bg jobs exit clear reset); do alias $c=" $c"; done

    – Will Norris Oct 14 '12 at 02:15
  • 8
    For lazy future readers like me who don't know how to do it yet: setopt HIST_IGNORE_SPACE in ~/.zshrc – Al.G. Apr 06 '16 at 17:27
  • does anyone have an idea why this doesn't work on mac? Since mac os switched to zsh as default shell I am unable to exclude any commands from the history :( – Sebastian Nov 07 '20 at 13:09
  • Btw HIST_IGNORE_SPACE is on by default in ohmyzsh, see history.zsh. – bravmi Apr 06 '23 at 08:17
35

If you're running the command over and over, you might want to use your shell's history ignore feature. Lets say you have secret.server.com that you ssh to, FTP files to, etc. that you don't want any line that mentions secret.server.com saved:

In bash you would set

HISTIGNORE="*secret.server.com*"

whereas in zsh the parameter is called

HISTORY_IGNORE="*secret.server.com*"

HISTIGNORE can contain multiple patterns, separated with colons. HISTORY_IGNORE specifies a single pattern but the pattern can contain alternatives, separated with pipes. Note that each pattern is matched against the full input line, so you might need to include a *. For example, the following will exclude fortune from the history, but will store fortune -l:

HISTIGNORE="*secret.server.com*:ytalk*:fortune"
HISTORY_IGNORE="*secret.server.com*|ytalk*|fortune"

With HISTIGNORE set, nothing matching the patterns you list will be saved to .bash_history/.zsh_history and even the up arrow key, which normally recalls your previous command, won't work if it matches your pattern.

Stephen Kitt
  • 434,908
25

In bash, use the HISTCONTROL variable.

Set it to HISTCONTROL=ignorespace (or HISTCONTROL=ignoreboth). From now, when you begin a line with a space, it will not be saved in the history. This avoids to include the not-to-be-disclosed-command in some configuration file.

Even like that it happens to forget to add the space and then want to go back. To delete an entry in the history, use history -d <index>, with index the number found with the history command (first column).

Mohammad
  • 688
shellholic
  • 6,255
17

The easiest way for a one-off is

$ $SHELL
$$ unset HISTFILE
$$ secret-command
$$ exit
$

where $ represents the prompt of the initial shell and $$ represents the prompt of the subshell. This works in bash and zsh; in ksh you need HISTFILE=/notwritable instead. Note that what matters is the value of HISTFILE when the history file is written, not when the command is run. Bash, ksh and zsh write to the history file when they exit (zsh can do it more often depending on configuration).

Alternatively, you can get bash or zsh to ignore lines matching certain patterns (this is already covered by other answers).

Another possibility in zsh is

% fc -p
% secret-command
% fc -P

fc -p pushes the current history list onto a stack and starts a new one that isn't associated with a save file. fc -P pops that unsaved history list off the stack and forgets about it altogether.

While a process is running, the command and its arguments (and on some systems its environment as well) will show up in the process table (ps output). Root can see past commands in the system accounting database, and can do more logging without your control. And there'll probably be all kinds of forensic evidence that can show roughly what command you ran (such as file modification times). So you don't get privacy against determined eavesdroppers.