Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

up vote 28 down vote accepted

In ZSH:

First set HIST_IGNORE_SPACE in your profile and then prefix the commands you don't want stored with a space.

From the man page, 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
share|improve this answer
    
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
    
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 2:15
add comment

If you're running the command over and over and your shell is bash, the HISTIGNORE variable will do this. 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:

HISTIGNORE="*secret.server.com*"

You can list multiple patterns with a colon separating them. Make sure sure you include a * if needed since in the below example, 'fortune' would be excluded, but 'fortune -l' would included':

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

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

share|improve this answer
3  
any chance this works in zsh? –  xenoterracide Jan 19 '11 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 '12 at 9:18
add comment

In bash, use the HISTCONTROL variable.

Set it to HISTCONTROL=ignorespace (or HISTCONTROL=ignoreboth). From now, when you begin a line with a space and 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 one entry in the history, use history -d <index>, with index the number found with the history command (first column).

share|improve this answer
    
Does this work in zsh? –  Gert Jan 19 '11 at 10:47
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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