33

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?
  • Note that if the goal is to hide passwords in command line arguments, they'll still be visible through ps – Benitok Apr 27 at 3:50
36

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

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

7

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
3

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.

2

I sometimes do

export HISTFILE=/dev/null
1

I see that no one wrote this method, so I will.

history -d $((HISTCMD-1)) && history -d <command or number of command>

history -d deletes the mentioned entry from the history.

HISTCMD stores the command_number of the command to be executed next. So, (HISTCMD-1) refers to the last executed command.

I did a short bash movie which shows how it works: https://asciinema.org/a/dtxpuovhty0obhln50soqypt2

After running the command and because it also deletes the command itself then going up with the arrow will not display the command, so if you plan on using that often, it would be best to create an alias:

alias hc='history -d $((HISTCMD-1)) && history -d $1'

Then you can do:

$ hc command

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.