I notice that when opening .bash_history
that it contains only the entries from my previous session, it seems that the current session is appended only on exit. Is there any way to prevent the current session from saving? Even crashing bash
is an option if one knows how to do that. I found that I can kill -9
the process, but if there is a better way I would love to know.

- 42,970
- 10
- 60
- 71

- 30,064
- 36
- 138
- 197
-
4A hackish way to accomplish this is to use `kill -9 $$`. By sending your terminal a SIGKILL, it doesn't get a chance to write to the history file. – jordanm Jan 28 '12 at 01:49
-
I've summed up everything and given more context in this answer https://stackoverflow.com/a/52208927/117471 – Bruno Bronosky Sep 06 '18 at 17:08
-
Also see [Execute command without keeping it in history](https://stackoverflow.com/q/8473121/608639), [How do I prevent commands from showing up in Bash history?](https://stackoverflow.com/q/6475524/608639), [Avoid to keep command in history](https://serverfault.com/q/48769) and friends. – jww Oct 27 '19 at 05:10
7 Answers
Unset the $HISTFILE
variable
$ unset HISTFILE
If HISTFILE is unset, or if the history file is unwritable, the history is not saved.
http://www.faqs.org/docs/bashman/bashref_106.html

- 148,182
- 27
- 114
- 126
-
1Thanks, this is good for Bash versions that nuke the whole history on -c. – dotancohen Jan 28 '12 at 08:29
-
What is the benefit of this vs `history -r`, because with that you'll lose everything you have done since you logged in. But with this you'll lose that and all that you are _going_ to do in the session. – Pylinux Feb 12 '18 at 13:17
-
@Pylinux If you are about to do something dodgy like supply passwords on the command line then you may want to ensure that what you are about to do doesn't get saved if you accidentally close the session before remembering to wipe the history. – Malvineous Nov 25 '20 at 07:14
-
1@Malvineous By far the most common usecase; not notising that the therminal has focus instead of the browser, accidentally writing the password in the terminal, and now I have to clean up after my self :-) – Pylinux Nov 26 '20 at 08:07
Perhaps more elegant than crashing bash would be to use the history -c
command to clear the history of the current session. Then, there's nothing to save (it even wipes itself from the history).

- 52,695
- 14
- 99
- 116
-
12
-
2It doesn't for me. Maybe it changed at some point? I'm using bash 4.2. – FatalError Jan 27 '12 at 19:52
-
-
13On bash 4.3.39, `history -c` doesn't just clear the history of the current session. It also clears the history read from the start of the history file when bash started. If you're about to close bash, this doesn't really matter. This is ONLY in the history buffer in memory - it doesn't actually wipe the history file (likely .bash_history). Even if you cleanly exit bash, it doesn't nuke the history file. So if you aren't exiting bash, you can follow it up with a `history -r` to re-read the history file. Sadly, `history -cr` doesn't do both at the same time. – user1902689 Jun 25 '15 at 03:51
-
@MichaelKrelin-hacker is being a bit *dramatic* with the word **nuke**. I just tested this on bash 3.2.57. Only the **history buffer in memory** is cleared. This means that up arrow will simply bell. But your `~/.bash_history` is unaffected. Any commands you use after that point go into your **history buffer in memory** and if you exit the shell, they will get **appended** to your `~/.bash_history`. It's like Will Smith in "Men in Black", not Will Smith in "Independence Day". – Bruno Bronosky Oct 19 '17 at 04:50
-
2@BrunoBronosky, I have no reason for disbelief, but I assume I did actually test it over five years ago before wiring the above. Not that I really remember. Maybe it has something to do with other settings and system defaults are different or have changed… – Michael Krelin - hacker Oct 19 '17 at 08:16
-
When using zsh, this cleared my history. I was able to dump contents from another shell into a backup. I get that this is a question for bash, though a warning might be useful. – MeowMeow Mar 20 '19 at 14:46
I know this is an old thread. Just wanted to add this for completion:
If you just want specific commands not to be saved check if HISTCONTROL
variable is set:
HISTCONTROL=ignoreboth
or
HISTCONTROL=ignorespace
Every command that starts with a leading space will not be put in the history.
Just my 2 cents.
From: man bash
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.

- 66,273
- 12
- 162
- 149

- 456
- 7
- 21
-
-
1`ignoreboth` will also not put duplicate lines, `ignorespace` will put duplicate lines. – confetti Aug 11 '19 at 10:08
Here is your bash history toolkit...
Exit bash without writing anything
kill -9 $$
This is hacky and aggressive. But it's a fun way to end an espionage session.
Clear history from memory
history -c
This clears memory of all history. If you hit the up arrow, you get nothing. Your $HISTFILE
is untouched. You can prove this with...
Reload history from disk
history -r
This rereads the $HISTFILE
and appends it to the history in memory (if there is any). You can do this after history -c
to regain the ability to Ctrl+R search or up arrow for previous commands. (You can do this instead of logging out and back in).
Note: If you didn't clear history first, this just appends to the current history in memory. This will obscure the history so that hitting the up arrow a few times will give you comfort in thinking that what you wanted to hide is gone. In reality it is just buried and will be written to disk unless it is deeper than your $HISTSIZE
or $HISTFILESIZE
.
Execute a command without including it in history
See th3penguinwhisperer's answer for the "set an option to have bash not record commands that are prefixed with a space" solution. I omit that (and the
ignoredups
anderasedups
) from my list because I think bash history should be journal and not a search index.
echo foo bar baz; history -d $(history 1)
This uses history -d
to delete an entry by number. Since only the first argument is used (and others are ignored) we can use the output of history 1
(which is identical to history | tail -n 1
) to get the number of the current entry.
Because bash oneliners are single history entries, you can do multiple commands like so:
echo foo; echo bar; echo baz; history -d $(history 1)
This also works:
echo foo \
bar \
baz; history -d $(history 1)
Even this works:
for x in foo bar baz; do
echo $x
done; history -d $(history 1)
Delete your password (a command, etc.) from your history
If all you are concerned about is getting rid of a single entry, you can use the previous example creatively. Use history
to find the number of the entry to delete. Then delete it by number. For example...
$ history
1 pwd
2 date
3 sudovisudo
4 hunter2
5 man history
6 help history
7 history
$ history -d 4
I hope I don't have to tell you this, but just in case: Don't grep history for your password. If you "really do" need to search history, do history | LESSHISTFILE=/dev/null less
, and explicitly do a /
search.
If you are really embarrassed and want there to be no record of you deleting something from history, you can combined this concept with the last.
history -d 4; history -d $(history 1)
Or to also get rid of the original mistake...
for n in "$(history 1)" 4 3; do history -d $n; done
Notice that you have to cycle over the entry numbers in decending order because each call to history -d
pops the entry out of the list and all subsequent entries' numbers decrease by 1. Also, you have to double quote the subshell because history 1
returns not just the number, but also the command and its arguments, and each would get a separate cycle in the for
loop. But at this point this is turning into a bash
lesson and I'll stop.

- 66,273
- 12
- 162
- 149
-
1Add a space in front of the command, if you want to execute a command without including it in history. (This feature can be enabled/disabled by `HISTCONTROL`) – Tianren Liu Jan 26 '22 at 14:34
-
@TianrenLiu that was addressed in [th3penguinwhisperer's answer](https://stackoverflow.com/a/26844730/117471) but since you mention it, I also updated my answer to link to their answer. – Bruno Bronosky Jan 26 '22 at 17:17
There's another option, similar to history -c
, but that does not wipe anything previous to the current session.
It is history -r
, which reloads the history from the HISTFILE
, like if you just logged in.
I don't know if this works or is available in any bash version previous to 4.3.11, but I though it would be useful to include it to the list.
Here's an example showing the difference between this command and the -c
one:
user@host:~$ # I Just logged in
user@host:~$ history | tail -n6 # this shows commands from the previous session, which ends at item 4682 as well as from the current one
4679 2014-08-23 17:15:29 # Previous session
4680 2014-08-23 17:15:33 # Still the previous session
4681 2014-08-23 17:15:37 # note the datetime
4682 2014-08-23 17:15:44 exit
4683 2014-08-23 17:17:25 # I Just logged in
4684 2014-08-23 17:19:54 history | tail -n6 # this shows the last command, and the ones from the previous session
user@host:~$ # This is a secret command, so I need to remove the traces of this session
user@host:~$ history -r
user@host:~$ history | tail -n5 # Note that I went back to item 4682, and there are no traces of history -r command
6242 2014-08-23 17:15:29 # Previous session
6243 2014-08-23 17:15:33 # Still the previous session
6244 2014-08-23 17:15:37 # note the datetime
6245 2014-08-23 17:15:44 exit
6246 2014-08-23 17:22:26 history | tail -n5 # Note that I went back to item 4682, and there are no traces of history -r command
user@host:~$ history -c # instead if I issue history -c
user@host:~$ history # everything disappears
5248 2014-08-23 17:23:13 history # everything disappears
user@host:~$

- 1,989
- 1
- 16
- 34
-
On bash 4.3.39, `history -r` Appends the contents of the history file to the current history list. Can't tell if that's what it was doing when you posted your answer... The history numbers jump around a lot between your commands, but don't double. – user1902689 Jun 25 '15 at 03:46
-
1Are you sure it appends them? In bash 4.3.11, the numbers jump forward because the counter is not reset, but it does not actually append the contents to the original ones. Indeed, if you log out and log in, the numbers go back to their original state. – Carles Sala Jun 25 '15 at 08:23
The following works for me.
export HISTFILE=/dev/null
Note that it has a space in front of it. Most modern distros would not add commands that are entered after a space to bash history. That will prevent that line also from appearing in your history.

- 1,906
- 1
- 17
- 15
-
This also makes your existing history unavailable in the session (for example, Ctrl+R reverse search will not work for existing history) – Blake Miller Oct 04 '22 at 23:23
-
Not on any Ubuntu installation, I have at home or at work. Using Ctrl + r still works. It just does not save the exiting terminal's history for this specific session. Nothing else is changed. – R J Oct 05 '22 at 05:43
That should do:
HISTFILE=
unset the HISTFILE.

- 138,757
- 24
- 193
- 173
-
Thank you, this works and I will use it for older Bash on some outdated servers. – dotancohen Jan 28 '12 at 08:28
-
You could also try: unset HISTFILE It certainly does the trick if you don't want anything of this session to be in the history. Otherwise see my comment above about HISTCONTROL. – th3penguinwhisperer Sep 01 '17 at 09:16