BASH 中的 history -a 与 history -w
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7344455/
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
history -a vs history -w in BASH
提问by J Spen
I'm writing my PROMPT_COMMAND="history -a;$PROMPT_COMMAND"but I still get some duplicates from different terminal sessions. It seems I still will get some duplicates but I get less using the command PROMPT_COMMAND="history -w;$PROMPT_COMMAND".
我正在写我的,PROMPT_COMMAND="history -a;$PROMPT_COMMAND"但我仍然从不同的终端会话中得到一些重复。似乎我仍然会得到一些重复,但我使用命令的次数减少了PROMPT_COMMAND="history -w;$PROMPT_COMMAND"。
I know I can do the history -a;history -c; history -rbut I don't want them all synced. I only want that to occur when I call history -n. I'm basically down to using either history -a or history -w but I can't seem to find the difference between the two. Which one would be better to have to avoid as many duplicates as possible.
我知道我可以做到,history -a;history -c; history -r但我不希望它们全部同步。我只希望在调用 history -n 时发生这种情况。我基本上只能使用 history -a 或 history -w ,但我似乎找不到两者之间的区别。最好避免尽可能多的重复。
回答by lesmana
You stated two questions:
你提了两个问题:
What is the difference between
history -aandhistory -w?
history -a和 和有什么不一样history -w?
history -awill appendyour current session historyto the content of the history file.
history -a将您当前的会话历史附加到历史文件的内容中。
history -wwill replacethe content of the history filewith your current session history.
history -w将用您当前的会话历史替换历史文件的内容。
Which one avoids more duplicates?
哪一个避免了更多的重复?
Theoretically neither. Neither -anor -wchecks for duplicates. Practically -wavoids more duplicates because it replaces the content of the history file. Any potential duplicate entries in the file are eliminated, along with anything else in the file.
理论上都不是。既不检查-a也不-w检查重复项。实际上-w避免了更多的重复,因为它替换了历史文件的内容。文件中任何潜在的重复条目以及文件中的任何其他内容都将被消除。
Read more about the historycommandin the bash manual.
在 bash 手册中阅读有关该history命令的更多信息。
Some more information about other possibilities to kill duplicates:
有关杀死重复项的其他可能性的更多信息:
The special bash variable HISTCONTROL=ignoredupsdoesn't help much here because that only eliminates duplicates as they are entered in the running shell. It does nothing to prevent existing duplicates in the current history or in the history file. Furthermore ignoredupsonly ignores commands that is a duplicate of the previous command, not if it is a duplicate of any command previously in history.
特殊的 bash 变量HISTCONTROL=ignoredups在这里没有多大帮助,因为它只会消除在运行的 shell 中输入的重复项。它不会阻止当前历史或历史文件中的现有重复项。此外,ignoredups仅忽略与前一个命令重复的命令,而不是如果它与历史记录中的任何命令重复。
Unfortunately not even the much promising HISTCONTROL=erasedupshelps much here. erasedupserases any history entry matching the currently entered command. Sadly, it only does so in the current session history. It does not search for duplicates in the history file. It will also not prevent duplicates from entering your session history when using history -n.
不幸的是,即使是很有希望的HISTCONTROL=erasedups东西在这里也没有多大帮助。erasedups删除与当前输入的命令匹配的任何历史条目。遗憾的是,它只在当前会话历史中这样做。它不会在历史文件中搜索重复项。它也不会阻止重复使用时进入您的会话历史记录history -n。
Read more about the special variable HISTCONTROLin the bash manual.
在 bash 手册中阅读有关特殊变量的HISTCONTROL更多信息。
回答by 1ac0
Use BASH variable PROMPT_COMMANDwith historyand using sortand uniqcommands:
使用bash变量PROMPT_COMMAND与history使用sort和uniq命令:
$> export PROMPT_COMMAND="history -a && history -c && sort ~/.bash_history | uniq > ~/.bash_history_new && rm ~/.bash_history && mv ~/.bash_history_new ~/.bash_history && history -r"
With this you don't need to set shopt -s histappendand do export HISTCONTROL=erasedups.
有了这个,你不需要设置shopt -s histappend和做export HISTCONTROL=erasedups。
This will keep your history and history file without duplicates on-the-fly. You can play with keeping history in order commands has been typed (hint: use line numbers in history, sort from specific line start position and after removing duplicates do sort again with line numbers).
这将保持您的历史和历史文件在运行中没有重复。您可以按照已输入的命令顺序保留历史记录(提示:在历史记录中使用行号,从特定行开始位置排序,并在删除重复项后再次使用行号进行排序)。
回答by reader_1000
you can avoid duplicates by using this
你可以通过使用这个来避免重复
export HISTCONTROL=ignoredups
You can look at this for more information:
您可以查看此了解更多信息:

