不要将当前的 bash 会话保存到历史记录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9039107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Don't save current bash session to history
提问by dotancohen
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.
我注意到当打开.bash_history
它只包含我上一个会话的条目时,似乎当前会话仅在退出时附加。有没有办法阻止当前会话保存?bash
如果有人知道如何做到这一点,即使崩溃也是一种选择。我发现我可以kill -9
处理这个过程,但如果有更好的方法,我很想知道。
回答by user123444555621
Unset the $HISTFILE
variable
取消设置$HISTFILE
变量
$ 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
如果未设置 HISTFILE,或者历史文件不可写,则不会保存历史。
http://www.faqs.org/docs/bashman/bashref_106.html
回答by FatalError
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).
也许比崩溃 bash 更优雅的是使用history -c
命令清除当前会话的历史记录。然后,没有什么可保存的(它甚至会从历史中抹去自己)。
回答by th3penguinwhisperer
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
如果您只想不保存特定命令,请检查是否HISTCONTROL
设置了变量:
HISTCONTROL=ignoreboth
或
HISTCONTROL=ignorespace
Every command that starts with a leading space will not be put in the history.
每个以前导空格开头的命令都不会放入历史记录中。
Just my 2 cents.
只有我的 2 美分。
回答by Carles Sala
There's another option, similar to history -c
, but that does not wipe anything previous to the current session.
还有另一个选项,类似于history -c
,但不会清除当前会话之前的任何内容。
It is history -r
, which reloads the history from the HISTFILE
, like if you just logged in.
它是history -r
,它从 重新加载历史记录HISTFILE
,就像您刚刚登录一样。
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.
我不知道这在 4.3.11 之前的任何 bash 版本中是否有效或可用,但我认为将它包含在列表中会很有用。
Here's an example showing the difference between this command and the -c
one:
这是一个示例,显示了此命令与该命令之间的区别-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:~$
回答by Michael Krelin - hacker
That should do:
那应该做:
HISTFILE=
unset the HISTFILE.
取消设置 HISTFILE。
回答by R J
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.
请注意,它前面有一个空格。大多数现代发行版不会将在空格后输入的命令添加到 bash 历史记录中。这将防止该行也出现在您的历史记录中。
回答by Bruno Bronosky
Here is your bash history toolkit...
这是您的 bash 历史记录工具包...
Exit bash without writing anything
不写任何东西就退出 bash
kill -9 $$
This is hacky and aggressive. But it's a fun way to end an espionage session.
这是hacky和侵略性的。但这是结束间谍活动的一种有趣方式。
Clear history from memory
从内存中清除历史记录
history -c
This clears memoryof allhistory. If you hit the up arrow, you get nothing. Your $HISTFILE
is untouched. You can prove this with...
这清除了所有历史的记忆。如果你点击向上箭头,你什么也得不到。你的完好无损。你可以用……来证明这一点$HISTFILE
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+Rsearch or up arrow for previous commands. (You can do this instead of logging out and back in).
这将重新读取$HISTFILE
并将其附加到内存中的历史记录(如果有)。您可以在history -c
重新获得Ctrl+R搜索或向上箭头以获取先前命令的能力之后执行此操作。(您可以执行此操作而不是注销并重新登录)。
Note:If you didn't clear history first, this just appends to the current history in memory. This will obscurethe 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
.
注意:如果您没有先清除历史记录,这只会附加到内存中的当前历史记录。这将掩盖历史,因此点击向上箭头几次会让您感到安慰,认为您想要隐藏的东西已经消失了。实际上,它只是被掩埋并会被写入磁盘,除非它比您的$HISTSIZE
或$HISTFILESIZE
.
Execute a command without including it in history
执行命令而不将其包含在历史记录中
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.
这用于history -d
按编号删除条目。由于只使用了第一个参数(其他参数被忽略),我们可以使用history 1
(与 相同history | tail -n 1
)的输出来获取当前条目的编号。
Because bash oneliners are single history entries, you can do multiple commands like so:
因为 bash oneliners 是单个历史条目,您可以执行多个命令,如下所示:
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
找到要删除的条目的数量。然后按编号删除。例如...
$ 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.
我希望我不必告诉您这一点,但以防万一:不要为您的密码 grep 历史记录。如果您“真的”需要搜索历史记录,请执行history | LESSHISTFILE=/dev/null less
并明确执行/
搜索。
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 decendingorder 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.
请注意,您必须按降序循环遍历条目编号,因为每次调用都会history -d
将条目从列表中弹出,并且所有后续条目的编号都减 1。此外,您必须将子 shell 用双引号括起来,因为不仅history 1
返回数字,还有命令及其参数,每个都将在循环中获得一个单独的for
循环。但在这一点上,这正在变成一个bash
教训,我会停下来。