Celeb Glow
updates | March 05, 2026

How to prevent a command in the zshell from being saved into history?

In Bash I know putting a space before a command prevents it from being kept in the history, what is the equivalent for the zshell?

1

2 Answers

Use the HIST_IGNORE_SPACE option.

setopt HIST_IGNORE_SPACE

man zshoptions

HIST_IGNORE_SPACE

Remove command lines from the history list when the first character on the line is a space, or when one of the expanded aliases contains a leading space. Note that 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. If you want to make it vanish right away without entering another command, type a space and press return.

11

If you desire more granular control over what's added to ZSH history, you can define the zshaddhistory function in .zshrc. The following definition uses a regex to define a pattern to ignore:

function zshaddhistory() { emulate -L zsh if ! [[ "$1" =~ "(^ |^ykchalresp|--password)" ]] ; then print -sr -- "${1%%$'\n'}" fc -p else return 1 fi
}

Note that the behavior from man zshopts under HIST_IGNORE_SPACE is still present:

Note that 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.

So to test it, you would have to hit an extra [Enter]. This removes the command both from the output of history, and also the ↑ arrow history.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy