2012-01-27 209 views
48

我注意到,打开.bash_history,它包含了我以前的对话中,仅有条目时,似乎当前会话仅在退出追加。有没有办法阻止当前会话保存?如果知道如何做到这一点,即使崩溃bash也是一种选择。我发现我可以在kill -9的过程中,但是如果有更好的方法我很想知道。当前bash命令不保存历史

+2

一个骇人的方法来完成这个是使用'kill -9 $$'。通过发送你的终端SIGKILL,它没有机会写入历史文件。 – jordanm 2012-01-28 01:49:27

回答

55

也许比bash的崩溃更优雅是使用history -c命令清除当前会话的历史记录。然后,没有什么可以保存的(它甚至从历史中抹去)。

+3

这也将会破坏histfile的内容。 – 2012-01-27 19:49:51

+1

它不适合我。也许它在某个时候改变了?我使用bash 4.2。 – FatalError 2012-01-27 19:52:49

+13

是的,它的核武器的历史3.2和不4.2 – 2012-01-27 20:05:34

53

取消设置$HISTFILE变量

$ unset HISTFILE 

如果HISTFILE没有设置,或者如果历史文件不可写,历史将不会保存。
http://www.faqs.org/docs/bashman/bashref_106.html

+0

谢谢,这对Bash版本来说非常好,它可以在-c上刻录整个历史。 – dotancohen 2012-01-28 08:29:02

+0

没有为我工作 – RSFalcon7 2013-04-12 19:49:32

+0

是什么这个VS'历史-r'利益,因为你会失去一切,你因为你登录完成。但有了这个,你会失去所有你是在会话中去做。 – Pylinux 2018-02-12 13:17:07

5

这应该做:

HISTFILE= 

取消设置HISTFILE。

+0

谢谢,这个工作,我会用它为老年人猛砸一些过时的服务器上。 – dotancohen 2012-01-28 08:28:10

+0

并没有为我工作 – RSFalcon7 2013-04-18 17:36:39

+0

您也可以尝试: 未设置HISTFILE 这肯定是卓有成效的,如果你不希望这个会议的任何事都要在历史中。否则,请参阅我上面有关HISTCONTROL的评论。 – th3penguinwhisperer 2017-09-01 09:16:21

10

还有另一种选择,类似于history -c,但不抹任何东西以前到当前会话。

这是history -r,从而重新加载从HISTFILE历史,就像如果你刚刚登陆,

我不知道这是否正常工作或提供任何庆典版之前的4.3.11,但我尽管将它列入清单会很有用。

这里是显示该命令和-c之间的差异的例子:

[email protected]:~$ # I Just logged in 
[email protected]:~$ 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 
[email protected]:~$ # This is a secret command, so I need to remove the traces of this session 
[email protected]:~$ history -r 
[email protected]:~$ 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 
[email protected]:~$ history -C# instead if I issue history -c 
[email protected]:~$ history # everything disappears 
5248 2014-08-23 17:23:13 history # everything disappears 
[email protected]:~$ 
+0

在bash 4.3.39上,'history -r'将历史文件的内容追加到当前历史列表中。当你发布你的答案时,无法判断它是在做什么......历史数字在你的命令之间跳来跳去,但是没有增加一倍。 – user1902689 2015-06-25 03:46:30

+0

你确定它附加了它们吗?在bash 4.3.11中,数字向前跳跃,因为计数器没有被重置,但它实际上并没有将内容追加到原始数据。事实上,如果您注销并登录,数字将回到原始状态。 – 2015-06-25 08:23:51

4

我下面的作品。

export HISTFILE=/dev/null 

注意,它在它前面的空间。大多数现代发行版不会添加在打开历史记录后输入的命令。这将阻止该行也出现在您的历史记录中。

7

我知道这是一个古老的线程。只是想添加此完成:

如果你只是想具体的命令不被保存检查,如果HISTCONTROL变量设置: HISTCONTROL = ignoreboth 或 HISTCONTROL = ignorespace

与领先开始的每个命令空间不会放在历史中。

只是我2美分。

+0

非常感谢企鹅! – dotancohen 2014-11-10 13:16:38

+0

这真是一个不错的选择! – 2017-06-13 19:48:48