89

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.

1

4 Answers 4

82

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
7
  • any chance @mmckinst answer works in zsh? or something like it? or perhaps any command that includes the command | somecommand Jan 19, 2011 at 10:57
  • AFAIK there is no equivalent of bash's HISTIGNORE in zsh.
    – dogbane
    Jan 19, 2011 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, 2011 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 Oct 14, 2012 at 2: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 6, 2016 at 17:27
34

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.

3
  • 4
    any chance this works in zsh? Jan 19, 2011 at 10:56
  • Is there an option to ignore only lines starting with fortune. Something like HISTIGNORE="^fortune*" to ignore fortune -l but keep myfortune?
    – Bernhard
    Oct 26, 2012 at 9:18
  • @xenoterracide It does, but the parameter is named differently. I just edited it into the answer.
    – n.st
    May 27, 2017 at 14:14
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).

2
  • 2
    Does this work in zsh?
    – Gert
    Jan 19, 2011 at 10:47
  • @Gert No, it doesn't.
    – miu
    Mar 21, 2022 at 1:10
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .