Do not save current bash session in history

I noticed that when opening .bash_history , that it contains only records from my previous session, it seems that the current session is added only on exit. Is there a way to prevent the current session from being saved? Even bash crashes is an option if you know how to do this. I found that I can kill -9 execute this process, but if there is a better way, I would love to know.

+43
bash
Jan 27 '12 at 19:44
source share
6 answers

Perhaps more elegant than bash crashes is to use the history -c command to clear the history of the current session. Then there is nothing that could be saved (even wipes itself out of history).

+50
Jan 27 '12 at 19:48
source share

Disconnect the $HISTFILE variable

 $ unset HISTFILE 

If HISTFILE is not installed, or if the history file is not possible, the history is not saved.
http://www.faqs.org/docs/bashman/bashref_106.html

+50
Jan 27 '12 at 19:48
source share

There is another option, similar to history -c , but which does not destroy anything preceding the current session.

This is history -r , which reloads history from HISTFILE , for example, if you are just logged in.

I do not know if this works or exists in any version of bash prior to 4.3.11, but although it would be useful to include it in the list.

Here is an example showing the difference between this command and -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:~$ 
+9
Aug 23 '14 at 15:30
source share

I know this is an old thread. Just wanted to add this to complete:

If you want certain commands not to be saved, check if the HISTCONTROL variable is set: HISTCONTROL = ignoreboth or HISTCONTROL = ignorespace

Each team starting with a leading space will not fit into the story.

Only my 2 cents.

+7
Nov 10 '14 at 13:15
source share

This should do:

 HISTFILE= 

disable HISTFILE.

+5
Jan 27 2018-12-12T00:
source share

The following works for me.

  export HISTFILE=/dev/null 

Note that there is a space in front of it. Most modern distributions will not add commands that are entered after a space in the bash history. This will prevent this line from appearing in your story.

+4
Aug 24 '14 at 1:02
source share



All Articles