This site says that on Linux you can add a space before a terminal command to keep it out of .bash_history.

This does not seem to work for Mac OS X. Does anyone know of a way to execute a single command on the Terminal without saving it to history?

I don't want to clear the history.

enter image description here

  • adding a ` ` (space) before a terminal command will not keep it out of the history
    – warren
    Aug 18 '11 at 16:20
  • 2
    @warren No, it does. You just have to enable the feature in bash.
    – goweon
    Aug 18 '11 at 16:33
  • @firebat - right: but since it's something you have to enable, it may or may not work everywhere :)
    – warren
    Aug 18 '11 at 16:35

Add the following line to ~/.bashrc

export HISTCONTROL=ignorespace

Then source ~/.bashrc to refresh the settings

This should enable that feature in bash. If it doesn't work, you might have to add it to ~/.bash_profile instead of ~/.bashrc since OS X loads them a bit differently than linux I think.

  • 2
    export HISTCONTROL=erasedups:ignorespace
    – goweon
    Aug 18 '11 at 16:34
  • 2
    But i think it's actually export HISTCONTROL=ignoredups:ignorespace
    – goweon
    Aug 18 '11 at 16:35
  • 3
    The "ignoredups" setting ignores repeated lines. Typing "ls" twenty times in a row only ends up with one of them in history. However, typing "ls" and then "ps" and then "ls" again will store "ls" and "ps" every time - unless you have bash 3 and set "erasedups". If that's set, no duplicates get entered in bash history at all. - cyberciti.biz/faq/bash-for-loop-array
    – cwd
    Aug 18 '11 at 18:39
  • 1
    Adding to .bash_profile works.
    – user51248
    Jun 7 '13 at 8:36
  • 2
    It seems that .bashrc is not used (OSX 10.13.5) but .bash_profile is.
    – B Seven
    Jul 9 '18 at 17:24

If you're not concerned about keeping the session active, this may work:

kill -9 $$

It will kill the current session instead of logging-out, which [theoretically] means your history will not be saved.

Your Answer

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

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