Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I use bash and I would like to avoid some commands being kept in the history.

  • Is it possible to do that for the next command only?
  • Is it possible to do that for the entire session?
share|improve this question

5 Answers 5

up vote 25 down vote accepted

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.
share|improve this answer
1  
+1 for the space trick :D –  petrus Apr 7 '11 at 19:00

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.

share|improve this answer

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

share|improve this answer

if you just want to remove the last command from the history, then you can hit up-arrow, then 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 ^U, and then up 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

share|improve this answer

I sometimes do

export HISTFILE=/dev/null
share|improve this answer

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.