Linux/bash:修订间差异
来自康健生活
创建页面,内容为“== History == Bash 解决多个终端History丢失的问题,让 shell 退出时是添加新记录,而不是覆盖原来的文件。 {{hc|.bashrc|{{shc|<syntaxhig…” |
|||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
== History == | == History == | ||
Bash 解决多个终端History丢失的问题,让 shell 退出时是添加新记录,而不是覆盖原来的文件。 | Bash 解决多个终端History丢失的问题,让 shell 退出时是添加新记录,而不是覆盖原来的文件。 | ||
{{hc|.bashrc | {{hc|.bashrc|<syntaxhighlight lang="bash"> | ||
shopt -s histappend</syntaxhighlight> | shopt -s histappend</syntaxhighlight>}} | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
source ~/.bashrc</syntaxhighlight>}} | source ~/.bashrc</syntaxhighlight>}} | ||
查看历史命令 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
history [选项] [历史命令保存文件]</syntaxhighlight>}} | |||
选项: | |||
* -c:清空历史命令; | |||
* -w:把缓存中的历史命令写入历史命令保存文件中。如果不手工指定历史命令保存文件,则放入默认历史命令保存文件 ~/.bash_history 中; | |||
{{shc|<syntaxhighlight lang="bash"> | |||
vi /etc/profile</syntaxhighlight>}} | |||
{{hc|profile|<pre> | |||
... | |||
HISTSIZE=1000 | |||
...</pre>}} | |||
还要注意一下,我们使用 history 命令查看的历史命令和 ~/.bash_history 文件中保存的历史命令是不同的。那是因为当前登录操作的命令并没有直接写入 ~/.bash_history 文件中,而是保存在缓存当中的,需要等当前用户注销之后,缓存中的命令才会写入 ~/.bash_history 文件中。 | |||
如果我们需要把内存中的命令直接写入 ~/.bash_history 文件中,而不等用户注销时再写入,就需要使用"-w"选项。命令如下: | |||
{{shc|<syntaxhighlight lang="bash"> | |||
history -w</syntaxhighlight>}} | |||
如果需要清空历史命令,则只需要执行如下命令: | |||
{{shc|<syntaxhighlight lang="bash"> | |||
history -c</syntaxhighlight>}} | |||