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.
There's more than a few ways to accomplish this.
Note: These options are not mutually exclusive; they can be used in any combination, or all at once.
HISTFILE shell variable.
According to the man page this will remove the shell's ability to automatically write any history to a file, regardless of if you set
HISTFILE to something after unsetting it.
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
cd aren't inserted into your history file.
Note: If you're using
shopt -s extglob, you can use extended globs in this variable, e.g.
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.
# With HISTCONTROL=ignorespace (or =ignoreboth) this line gets written to history $ mycommand # But this line does not $ mycommand
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
# don't record history when the window is closed trap 'unset HISTFILE; exit' SIGHUP
SIGKILL to your shell's PID 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.
$$ expands to the PID of the shell process executing the command)
kill -9 $$
Your shell's history is saved in the file indicated by the
HISTFILE variable. So:
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).
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):
Either of these work fine in Bash on Fedora
Eli already gave you the correct answer for Bash which is to set
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 15000to reset scroll-back buffer size.