无限的 Bash 历史
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9457233/
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
Unlimited Bash History
提问by Francis Haart
I want my .bash_history
file to be unlimited. e.g. So I can always go back and see how I built/configured something, or what that nifty command was, or how some command broke something weeks ago. How do I change this setting?
我希望我的.bash_history
文件不受限制。例如,所以我总是可以回去看看我是如何构建/配置某些东西的,或者那个漂亮的命令是什么,或者几周前某个命令是如何破坏某些东西的。如何更改此设置?
采纳答案by Lri
Set HISTSIZE
and HISTFILESIZE
in .bashrc to an empty string:
在 .bashrc 中将HISTSIZE
和设置HISTFILESIZE
为空字符串:
HISTSIZE=
HISTFILESIZE=
In bash 4.3 and lateryou can also use HISTSIZE=-1 HISTFILESIZE=-1
:
在bash 4.3 及更高版本中,您还可以使用HISTSIZE=-1 HISTFILESIZE=-1
:
n. Setting HISTSIZE to a value less than zero causes the history list to be
unlimited (setting it 0 zero disables the history list).
o. Setting HISTFILESIZE to a value less than zero causes the history file size
to be unlimited (setting it to 0 causes the history file to be truncated
to zero size).
回答by fotinakis
After many large, ugly iterations and weird edge cases over the years, I now have a concise section of my .bashrcdedicated to this.
多年来,经过许多大型、丑陋的迭代和奇怪的边缘情况,我现在有一个专门用于此的.bashrc的简洁部分。
First, you must comment out or remove this section of your .bashrc(default for Ubuntu). If you don't, then certain environments (like running screen
sessions) will still truncate your history:
首先,您必须注释掉或删除 .bashrc 的这一部分(Ubuntu 的默认设置)。如果不这样做,那么某些环境(如运行screen
会话)仍会截断您的历史记录:
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
# HISTSIZE=1000
# HISTFILESIZE=2000
Second, add thisto the bottom of your .bashrc:
其次,将其添加到 .bashrc 的底部:
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
Note: every command is written immediately after it's run, so if you accidentally paste a password you cannot just "kill -9 %%" to avoid the history write, you'll need to remove it manually.
注意:每个命令都会在运行后立即写入,因此如果您不小心粘贴了密码,您不能仅仅通过“kill -9 %%”来避免历史写入,您需要手动删除它。
Also note that each bash session will load the full history file in memory, but even if your history file grows to 10MB (which will take a long, longtime) you won't notice much of an effect on your bash startup time.
另请注意,每个 bash 会话都将在内存中加载完整的历史文件,但即使您的历史文件增长到 10MB(这将花费很长时间),您也不会注意到对 bash 启动时间有太大影响。
回答by simont
As J?rg Beyer mentioned above, HISTSIZE
and HISTFILESIZE
are key.
为J?RG拜尔如上所述,HISTSIZE
和HISTFILESIZE
是关键。
In addition, you should definitelycheck out the environmental variable HISTCONTROL
, which lets you do cool things like not store duplicate history commands (HISTCONTROL=erasedups
). There's no point having unlimited history if you have to browse through hundreds of lines of cd ..
or similar.
此外,你一定要检查环境变量HISTCONTROL
,它可以让你做一些很酷的事情,比如不存储重复的历史命令 ( HISTCONTROL=erasedups
)。如果您必须浏览数百行cd ..
或类似的行,那么拥有无限的历史记录是没有意义的。
Links: here, and working with bash history. The bash Variable FAQ is also worth browsing.
链接:here,以及使用 bash history。bash 变量常见问题也值得浏览。
回答by J?rg Beyer
There are (at least) two relevant env vars here:
这里有(至少)两个相关的环境变量:
- HISTSIZE: the number of entries in the history file
- HISTFILESIZE: the number of lines in the history file
- HISTSIZE:历史文件中的条目数
- HISTFILESIZE:历史文件中的行数
I think that we can agree that the term unlimitedis often the same as very big(or do you have unlimited file storage?). So just set the values very large.
我认为我们可以同意“无限制”一词通常与“非常大”相同(或者您是否拥有无限制的文件存储?)。所以只需将值设置得非常大。