如何限制 bash 中的历史项目数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29995818/
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
how do I limit the number of history items in bash?
提问by Filype
in a recent tweet I've seen it's been said that there are more passwords stored in history than in /etc/shadow
- the question is: how do I limit the number of items in history, as I don't use it that often anyway?
在最近的一条推文中,我看到有人说历史中存储的密码比历史中存储的密码多/etc/shadow
- 问题是:我如何限制历史中的项目数量,因为我不经常使用它?
回答by Charles Duffy
This is controlled by the HISTSIZE and HISTFILESIZE built-in shell variables. Quoting from the documentation in man bash
:
这是由 HISTSIZE 和 HISTFILESIZE 内置 shell 变量控制的。从文档中引用man bash
:
HISTSIZE
- The number of commands to remember in the command history (see HISTORY below). If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.
HISTFILESIZE
- The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines by removing the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history file is truncated to zero size. Non-numeric values and numeric values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after reading any startup files.
HISTSIZE
- 命令历史中要记住的命令数量(参见下面的 HISTORY)。如果值为 0,则命令不会保存在历史列表中。小于零的数值会导致每个命令都保存在历史列表中(没有限制)。在读取任何启动文件后,shell 将默认值设置为 500。
HISTFILESIZE
- 历史文件中包含的最大行数。当这个变量被赋值时,如果有必要,历史文件被截断,通过删除最旧的条目来包含不超过该数量的行。历史文件在 shell 退出时写入后也会被截断到这个大小。如果值为 0,则历史文件将被截断为零大小。非数字值和小于零的数字值禁止截断。在读取任何启动文件后,shell 将默认值设置为 HISTSIZE 的值。
~/.bashrc
is an appropriate place to set these values. They don't need to be exported, since they're internal to the shell:
~/.bashrc
是设置这些值的合适位置。它们不需要导出,因为它们在 shell 内部:
HISTFILESIZE=25
...will do fine, no export
needed.
...会做的很好,export
不需要。
回答by David Lio
You can limit the number of items in history by adding this line to your ~/.bashrc file:
您可以通过将此行添加到您的 ~/.bashrc 文件来限制历史记录中的项目数:
# remember the last 1000 commands in history
HISTSIZE=1000
Some additional bash history parameters that you might find interesting:
您可能会感兴趣的一些额外的 bash 历史参数:
# Limit the size of the ~/.bash_history file
HISTFILESIZE=2000
# append to the history file, don't overwrite it
shopt -s histappend
# Avoid duplicate entries
export HISTCONTROL=ignoredups:erasedups
From the command line, you can also enter a history search mode by pressing Control + R. As you start typing, the history search will suggest items.
从命令行,您还可以通过按 Control + R 进入历史搜索模式。当您开始输入时,历史搜索将建议项目。
Additionally, if you use the history
command, you can execute items by number (e.g. !99
would execute the 99th command in history). !!
will also execute the last command previously executed.
此外,如果您使用该history
命令,您可以按编号执行项目(例如,!99
将执行历史上第 99 个命令)。 !!
还将执行之前执行的最后一条命令。
Many programs are good about keeping passwords out of history. If you do notice an entry you would like to delete, however, you can do so like this:
许多程序都擅长将密码排除在历史之外。但是,如果您确实注意到要删除的条目,您可以这样做:
# say we start with an empty bash command history
bash-3.2$ history
1 history
# enter a command that requires a password
bash-3.2$ sudo rm -i some_file
Password:
# accidentally ^C and type your password
# into the prompt and hit enter
bash-3.2$ secret_password
bash: secret_password: command not found
# your password is now there for all to
# see in your bash history
bash-3.2$ history
1 history
2 sudo rm -i some_file
3 secret_password
4 history
# first option to fix it, delete the numbered entry from
# history and write to your ~/.bash_history file
bash-3.2$ history -d 3
bash-3.2$ history -w
# entry 3 will be removed entirely from your command history
bash-3.2$ history
1 history
2 sudo rm -i some_file
3 history
4 history -d 3
5 history -w
6 history
# the second option is to clear the entire history
# and write the changes to disk
bash-3.2$ history -c
bash-3.2$ history -w
# it's now pretty obvious that your history has been
# scrubbed clean, but at least your password is history!
bash-3.2$ history
1 history -w
2 history
source for above example:http://rawsyntax.com/blog/learn-bash-remove-commands-from-your-history/
以上示例的来源:http: //rawsyntax.com/blog/learn-bash-remove-commands-from-your-history/
回答by Ray
Set history file size with something like:
使用以下内容设置历史文件大小:
HISTFILESIZE=1024
Disable history with:
禁用历史记录:
HISTFILESIZE=0