Eri*_* Hu 6 bash command-history
大约每天一次,当我在 bash 中搜索历史项目时会遇到问题。操作该项目有时会删除历史条目,有时不会。
这是发生这种情况的一个案例:
$ls foo
ls: No such file or directory
# (ctrl-r)ls foo(tab)
$ ls foo
# (ctrl-a)(ctrl-k)
$
# (ctrl-r)ls foo
# (no matches)
Run Code Online (Sandbox Code Playgroud)
对这里发生的事情的解释是什么?
Ark*_*zyk 10
我认为这种行为的原因是 Bash 如何处理先前历史行的修改。诸如previous-history( C- p) 或reverse-search-history( C- r) 之类的命令是获取以前的历史条目:
previous-history (C-p)
Fetch the previous command from the history list, moving back in the list.
Run Code Online (Sandbox Code Playgroud)
当以前的历史项是取它的印刷,就好像它是类型化。现在,您可以只修改它但不能执行(如您的示例)或修改并执行它。当你执行它时,你调用accept-line:
accept-line (Newline or Return)
Accept the line regardless of where the cursor is. If this line is
non-empty, add it to the history list according to the setting of
the HISTCONTROL and HISTIGNORE variables. If this line is a
modified history line, then restore the history line to its
original state.
Run Code Online (Sandbox Code Playgroud)
按下后Return,修改后的行将被保存到历史记录中,而原始行将保持不变。但是让我们考虑一下如果您只是修改获取的行而不按下会发生什么Return- 它已修改但未执行,因此accept-line不会被调用。发生的事情是原来的历史线被修改了。
要在实践中看到这一点,请将此行添加到您的~/.inputrc并启动新的子shell:
set mark-modified-lines On
Run Code Online (Sandbox Code Playgroud)
现在让我们按照您的示例进行操作:
$ echo April # 0) press Return - `accept-line` is called
# 1. press C-r and type `April' and Tab - you will see this again because
# `echo April' is in history:
$ echo April
# 2. now kill this line using C-k or C-u. C-r `April' doesn't work anymore
# because there is no `echo April' in the history
# 3. DON'T PRESS RETURN HERE! Press Up arrow first a couple of times and
# press Return to invoke a different command, it can be anything you had
# in your history but just no Return here
# 4. now, see `history', somewhere there you will see the empty string. It
# may look a bit different depending on your HISTTIMEFORMAT but note
# there is an asterisk next to the history line number and a command is
# missing. It's missing because it has been modified and saved in 2).
$ history
(...)
483* 2015-04-12 02:36:19
Run Code Online (Sandbox Code Playgroud)
正如您在问题的第二条评论中注意到的那样,如果您在 2) 中按下 Return 键,您将不会修改在点 0) 输入的原始命令,并且可以使用C-调用它r。
好的,我意识到乍一看这可能会令人困惑且难以理解。如果您有任何问题,请回来,我会尽量说得更清楚。
| 归档时间: |
|
| 查看次数: |
336 次 |
| 最近记录: |