我注意到,当打开.bash_history它只包含我上一个会话中的条目时,似乎当前会话仅在退出时附加.有没有办法阻止当前会话保存?bash如果知道如何做到这一点,即使崩溃也是一种选择.我发现我可以完成kill -9这个过程,但是如果有更好的方法我会很高兴知道.
use*_*621 73
取消设置$HISTFILE变量
$ unset HISTFILE
Run Code Online (Sandbox Code Playgroud)
如果未设置HISTFILE,或者历史文件不可写,则不保存历史记录.
http://www.faqs.org/docs/bashman/bashref_106.html
Fat*_*ror 73
也许比崩溃bash更优雅的是使用该history -c命令来清除当前会话的历史记录.然后,没有什么可以保存(它甚至从历史中抹去).
th3*_*rer 13
我知道这是一个老线程.只想添加此内容以完成:
如果您只想要保存特定命令,请检查是否HISTCONTROL设置了变量:
HISTCONTROL=ignoreboth
或
HISTCONTROL=ignorespace
以前导空格开头的每个命令都不会放入历史记录中.
只需2美分.
Car*_*ala 12
还有另一个选项,类似于history -c,但不会擦除当前会话之前的任何内容.
它是history -r,重新加载历史记录HISTFILE,就像你刚刚登录一样.
我不知道这是否有效,或者在4.3.11之前的任何bash版本中都可用,但我将它包含在列表中会很有用.
这是一个显示此命令与-c一个命令之间差异的示例:
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:~$
Run Code Online (Sandbox Code Playgroud)
以下适用于我.
export HISTFILE=/dev/null
Run Code Online (Sandbox Code Playgroud)
请注意,它前面有一个空格.大多数现代发行版都不会添加在空格之后输入的命令来打击历史记录.这样可以防止该行出现在您的历史记录中.
这是您的bash历史记录工具包...
kill -9 $$
Run Code Online (Sandbox Code Playgroud)
这是hacky和侵略性的。但这是结束间谍活动的一种有趣方式。
history -c
Run Code Online (Sandbox Code Playgroud)
这将清除内存的所有历史。如果您按下向上箭头,您将一无所获。您$HISTFILE没有动过。你可以用...证明
history -r
Run Code Online (Sandbox Code Playgroud)
这会重新读取$HISTFILE并将其附加到内存中的历史记录(如果有的话)。您可以在执行此操作后history -c重新获得对以前命令Ctrl+ R搜索或向上箭头的功能。(您可以执行此操作,而不是注销并重新登录)。
注意:如果您没有先清除历史记录,则只会附加到内存中的当前历史记录。这将掩盖历史,因此几次按下向上箭头可以让您感到想隐藏的东西已经消失了。实际上,除非它比您$HISTSIZE或者更深,否则它只是被掩埋并将被写入磁盘$HISTFILESIZE。
echo foo bar baz; history -d $(history 1)
Run Code Online (Sandbox Code Playgroud)
这用于history -d按数字删除条目。由于仅使用第一个参数(而其他参数则被忽略),因此我们可以使用输出history 1(与相同history | tail -n 1)来获取当前条目的编号。
由于bash oneliners是单个历史记录条目,因此您可以执行多个命令,如下所示:
echo foo; echo bar; echo baz; history -d $(history 1)
Run Code Online (Sandbox Code Playgroud)
这也适用:
echo foo \
bar \
baz; history -d $(history 1)
Run Code Online (Sandbox Code Playgroud)
即使这样有效:
for x in foo bar baz; do
echo $x
done; history -d $(history 1)
Run Code Online (Sandbox Code Playgroud)
如果您只关心消除单个条目,则可以创造性地使用前面的示例。使用history找到要删除的条目的数量。然后按编号删除它。例如...
$ history
1 pwd
2 date
3 sudovisudo
4 hunter2
5 man history
6 help history
7 history
$ history -d 4
Run Code Online (Sandbox Code Playgroud)
希望我不必告诉您这一点,但以防万一:不要grep您的密码历史记录。如果您“确实要”搜索历史记录,请执行history | LESSHISTFILE=/dev/null less,并明确进行/搜索。
如果您真的很尴尬,并且不想有任何删除历史记录的记录,则可以将此概念与上一个合并。
history -d 4; history -d $(history 1)
Run Code Online (Sandbox Code Playgroud)
或者也要摆脱原来的错误...
for n in "$(history 1)" 4 3; do history -d $n; done
Run Code Online (Sandbox Code Playgroud)
请注意,您必须按降序循环输入条目号,因为每次调用都会history -d将条目从列表中弹出,而所有后续条目的号都会减少1。此外,您必须用双引号括住子外壳,因为history 1返回的不仅是数字,以及命令及其参数,并且每个都将在循环中获得单独的for循环。但是现在这变成了一个bash教训,我将停止。
| 归档时间: |
|
| 查看次数: |
44367 次 |
| 最近记录: |