Avoid to keep command in history

Solution 1:

and i just remembered another answer, this one is the actual answer to your question.

if you have "ignorespace" in HISTCONTROL, then bash wont remember any line beginning with a space character. it won't appear even in the current shell's history, let alone be saved to $HISTFILE.

e.g. I have export HISTCONTROL='ignoreboth:erasedups' in my ~/.bashrc

here's the details from the bash man page:

HISTCONTROL
  A colon-separated list of values controlling how commands are
  saved on the history list.  If the list of values includes
  ignorespace, lines which begin with a space character are not
  saved in the history list.  A value of ignoredups causes lines
  matching the previous history entry to not be saved.  A value of
  ignoreboth is shorthand for ignorespace and ignoredups.  A value
  of erasedups causes all previous lines matching the current line
  to be removed from the history list before that line is saved.
  Any value not in the above list is ignored.  If HISTCONTROL is
  unset, or does not include a valid value, all lines read by the
  shell parser are saved on the history list, subject to the value
  of HISTIGNORE.  The second and subsequent lines of a multi- line
  compound command are not tested, and are added to the history
  regardless of the value of HISTCONTROL.

Solution 2:

Here's a few history lessons I learned from googling:

You can set the history to ignore certain strings. In this example, I've ignored the commands ls, passwd and any command prefixed by the character space.

export HISTIGNORE="ls:passwd: "

To disable history for your session you can issue:

export HISTSIZE=0

Reference


Solution 3:

If you just want to remove the last command from the history, then you can hit Up-arrow, followed by Ctrl - U (erase to beginning of line), then Up-arrow again.

You can erase any individual line from the history like this. Just use up-arrow or search or whatever to make it the current command line being edited, then press Ctrl - U, and then Up-arrow or Down-arrow to move to another history line.

I discovered this by accident one day years ago. I have no idea whether it's documented or not.

Generally, though, it's less hassle to just not save the current shell's history at all with:

HISTFILE=/dev/null

Solution 4:

You could unset the HISTFILE variable. From man bash:

  HISTFILE
         The name of the file in which command history is saved (see HIS-
         TORY below).  The default value is ~/.bash_history.   If  unset,
         the  command  history  is  not  saved  when an interactive shell
         exits.

I don't think this prevents the command from appearing in the local shell history before the shell exits.


Solution 5:

I sometimes do

export HISTFILE=/dev/null