如何在交互式 shell 中合并重复的命令历史记录?

fro*_*hem 6 bash command-history macos

当您尝试在 \xc2\xa0 交互式 shell 会话\nby\xc2\xa0 中查找以前使用过的命令时\xe2\x86\x91\xc2\xa0(up\xe2\x80\xafarrow),\xc2\xa0 可能会得到类似的内容

\n
$ls         # 1st time push `up arrow`\n$ls         # 2nd time push `up arrow`\n$ls         # 3rd time push `up arrow`\n$ls         # 4th time push `up arrow`\n$ls         # 5th time push `up arrow`\n$ls         # 6th time push `up arrow`\n$make       # 7th time push `up arrow`\n$make       # 8th time push `up arrow`\n$make       # 9th time push `up arrow`\n$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"   # Bingo!\n
Run Code Online (Sandbox Code Playgroud)\n

如果是这样的话我会更好:

\n
$ls         # 1st time push `up arrow`\n$make       # 2th time push `up arrow`\n$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"   # Bingo!\n
Run Code Online (Sandbox Code Playgroud)\n

因为重复的历史记录通常是没有用的。

\n

我怎样才能让 Bash 做到这一点?

\n

jor*_*anm 6

您可以通过设置ignoredupsHISTCONTROL 变量来完成此操作:

\n
HISTCONTROL="ignoredups"\n
Run Code Online (Sandbox Code Playgroud)\n

可选择导出它,

\n
export HISTCONTROL="ignoredups"\n
Run Code Online (Sandbox Code Playgroud)\n

使其成为环境变量。\xc2\xa0\n来自bash(1) man\xe2\x80\xafpage:

\n
\n

HISTCONTROL

    \n以冒号分隔的值列表,控制如何将命令\n保存在历史记录列表中。\xc2\xa0\n如果值列表包含,\n以空格ignorespace字符开头的行不会保存在历史记录\n列表中。\xc2 \xa0 的值 会导致与上一个历史条目匹配的行\n不被保存。\xc2\xa0 A\xc2\xa0 的值是和\xc2\xa0 的简写\n 。\xc2\xa0 A\xc2\xa0 的值会导致所有\n与当前行匹配的前一行将在保存该行之前从历史记录列表中删除。\xc2\xa0 任何不在上述列表中的值都将被忽略。\xc2\xa0 如果未设置,或不包含有效的值\n值,shell解析器读取的所有行都保存在历史列表中,\n以 的值为准 。\xc2\xa0\n多行复合命令的第二行和\n后续行未测试,\n添加无论 \n 的值如何,都会记录到历史记录。ignoredupsignorebothignorespaceignoredupserasedupsHISTCONTROLHISTIGNOREHISTCONTROL

\n

\n

这正是问题所要求的。\xc2\xa0\n如果\xc2\xa0the\xc2\xa0用户输入命令ruby\xc2\xa0\xe2\x80\xa6,\n make, make, make, ls, ls, ls, ls,ls和\xc2\xa0 ls\n(作为单独的连续行) ,\n则历史列表将为ruby\xc2\xa0\xe2\x80\xa6, make, ls。\xc2\xa0\n按\xc2\xa0 \xe2\x86\x91\xc2\xa0(up\xe2\x80\xafarrow)3 次\n将返回\xc2\xa0\xc2\xa0ruby命令。

\n