无限的Bash历史[关闭]

本文翻译自:Unlimited Bash History [closed]

I want my .bash_history file to be unlimited. 我希望我的.bash_history文件无限制。 eg 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? 如何更改此设置?


#1楼

参考:https://stackoom.com/question/dgG1/无限的Bash历史-关闭


#2楼

Set HISTSIZE and HISTFILESIZE in .bashrc to an empty string: 将.bashrc中的HISTSIZEHISTFILESIZE设置为空字符串:

HISTSIZE= 
HISTFILESIZE=

In bash 4.3 and later you 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).

#3楼

After many large, ugly iterations and weird edge cases over the years, I now have a concise section of my .bashrc dedicated to this. 经过多年来的许多大型,丑陋的迭代和奇怪的边缘情况,我现在有一个专门用于此的.bashrc的简洁部分。

First, you must comment out or remove this section of your .bashrc (default for Ubuntu). 首先,您必须注释掉或删除.bashrc的此部分 (Ubuntu的默认值)。 If you don't, then certain environments (like running screen sessions) will still truncate your history: 如果不这样做,那么某些环境(如运行screen会话)仍会截断您的历史记录:

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
# HISTSIZE=1000
# HISTFILESIZE=2000

Second, add this to 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. 注意:每个命令在运行后立即写入,因此如果您不小心粘贴了密码,您不能只是“杀死-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, long time) you won't notice much of an effect on your bash startup time. 另请注意,每个bash会话都会在内存中加载完整的历史记录文件,但即使您的历史文件增长到10MB(这需要很长很长时间),您也不会注意到对bash启动时间有太大影响。


#4楼

There are (at least) two relevant env vars here: 这里至少有两个相关的环境变量:

  • HISTSIZE: the number of entries in the history file HISTSIZE:历史文件中的条目数
  • HISTFILESIZE: the number of lines in the history file HISTFILESIZE:历史文件中的行数

I think that we can agree that the term unlimited is often the same as very big (or do you have unlimited file storage?). 我认为我们可以同意无限期这个词通常与非常大 (或者你有无限的文件存储?)。 So just set the values very large. 所以只需设置非常大的值。


#5楼

As Jörg Beyer mentioned above, HISTSIZE and HISTFILESIZE are key. 正如HISTSIZE在上面提到的, HISTSIZEHISTFILESIZE是关键。

In addition, you should definitely check out the environmental variable HISTCONTROL , which lets you do cool things like not store duplicate history commands ( HISTCONTROL=erasedups ). 另外,你一定要查看环境变量HISTCONTROL ,它可以让你做一些很酷的事情,比如不存储重复的历史命令( HISTCONTROL=erasedups )。 There's no point having unlimited history if you have to browse through hundreds of lines of cd .. or similar. 如果您必须浏览数百行cd ..或类似内容,那么无限的历史就没有意义了。

Links: here , and working with bash history . 链接: 在这里 ,并使用bash历史记录 The bash Variable FAQ is also worth browsing . bash Variable FAQ也值得浏览

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值