Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

More than once I've accidentally run a number of commands and polluted my bash history. How do I close my terminal without saving my bash history? I'm using Fedora.

share|improve this question
add comment

6 Answers

up vote 16 down vote accepted

Your shell's history is saved in the file indicated by the HISTFILE variable. So:

unset HISTFILE

This also applies to zsh, but not to ksh which keeps saving to the file indicated by $HISTFILE when the shell starts (and conversely, you decide to save your history in ksh once you've started the shell).

share|improve this answer
    
I can use this before i log out after i polluted the history? and its ust for this session? i dont need to set HISTFILE next time i log in? (just say if this is correct or incorrect) –  acidzombie24 Nov 21 '11 at 0:51
    
@acidzombie24 That is correct, changes to environment variables are not saved across sessions unless you store the changes explicitly, e.g. in rc files. –  jw013 Nov 21 '11 at 1:44
add comment

Short answer:

Type this at the prompt:

$ kill -9 $$

This will kill your shell right away without the shell being able to do anything such as trap the signal, save history, execute ~/.bash_logout, warn about stopped jobs, or any of that good stuff.

Long answer:

Note: These options are not mutually exclusive; they can be all used at once.

Option 1:

If you're a perfectionist when it comes to cluttering up your history file, then what you can do is modify the HISTIGNORE variable to include globs of commands you don't want recorded. For instance, if you add HISTIGNORE='ls*:cd*' to your ~/.bashrc then any instance of ls and cd aren't inserted into your history file.

Option 2:

If you want to control on a command-by-command basis what commands get left out of your history, you can set HISTCONTROL='ignorespace' which will omit any command lines starting with a space. Using ignoreboth will also omit repeated lines. Then, hitting the space bar before you enter a command will cause it to not show up in your history file.

Option 3:

If you just want to make it so when you close the terminal the shell exits immediately, you can trap the signal the terminal program sends the shell (xterm, for instance sends SIGHUP then waits for the shell to exit) and make exit without saving the history when it receives this signal. Add this to your ~/.bashrc:

# don't record history when the window is closed
trap 'unset HISTFILE; exit' SIGHUP
share|improve this answer
    
each answer is great, i dont know which i should accept –  acidzombie24 Nov 21 '11 at 1:53
2  
+1 for the 'kill -9 $$' method. That's certainly one way around situations where HISTIGNORE, etc are set as read-only. –  Corey Henderson Nov 21 '11 at 5:28
5  
I don't think it's a good idea to promote using kill -9 for anything except dire situations when everything else has failed. –  Christoffer Hammarström Nov 21 '11 at 16:51
    
Sending SIGKILL never a good idea as a standard operating procedure. Use the proper procedures, like unsetting HISTFILE. –  Arcege Nov 21 '11 at 23:26
    
@Arcege TMTOWTDI. –  amphetamachine Nov 22 '11 at 3:16
add comment

I'm surprised to see no one has suggested history -c immediately prior to exit. IINM (I'm no expert) that will do nicely.

share|improve this answer
2  
You're on the right track: history -r will re-read the history file, so saving becomes a no-op. –  Simon Richter Nov 21 '11 at 9:47
add comment

Not sure why you care about your command history so much. If you need certain commands often, you might have more fun if you define aliases for them so you can get them back with two keystrokes rather than having to look for them in the history.

share|improve this answer
add comment
  1. Eli already gave you the correct answer for Bash which is to set HISTSIZE=0.

  2. I would just add the method to do it for GNU screen. Press Ctrl+A (screen escape sequence) followed by :scrollback 0. This will delete scroll-back history. Now you can immediately do :scrollback 15000 to reset scroll-back buffer size.

share|improve this answer
    
When did the OP say they were using screen(1)? –  amphetamachine Nov 21 '11 at 2:47
    
He didn't. But, you could be running Bash inside a screen session and HISTSIZE=0 will still leave your activity details in screen's scroll-back buffer. So, if you really want it clean, you have to do scrollback=0 as well. –  GSBabil Nov 21 '11 at 3:37
    
The scrollback buffer is cleared when the window closes (i.e. when the shell exits). What about if you typed Ctrl-a H? Will it kill the logfile? –  amphetamachine Nov 21 '11 at 7:23
    
@amphetamachine: nope, it will only begin/end logging of current window. Neighther Ctrl+a H nor Ctrl+a :clear will remove history. You need Ctrl+a :scrollback 0. You can test it yourself. Start a fresh screen session. Now do a cat /etc/passwd. Now, do any of the above - Ctrl+a H or Ctrl+a :clear. Now, try copying from screen buffer by donig a Ctrl+a [ followed by up-arrow to go up and see how far you can go to copy. If you have done a Ctrl+a :scrollback followed by a clear, you would only go as far as you can see in current window since there wont be any scrollback buffer. –  GSBabil Nov 21 '11 at 12:15
add comment

There are two environment variables that bash uses to determine the history file and how many lines to write to it when the shell exits.

You can throw away your session's history with either of these (set during the session you want to omit from your history file):

HISTFILE=/dev/null

or

HISTSIZE=0

Either of these work fine in Bash on Fedora

share|improve this answer
add comment

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.