Is there any way to keep a command from being added to your history?
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
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*"
You can list multiple patterns with a colon separating them. 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.
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).