私のbash履歴からアイテムが削除される原因は何ですか?

私のbash履歴からアイテムが削除される原因は何ですか?

1 日に 1 回、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)

ここで何が起こっているのかについての説明は何ですか?

ベストアンサー1

私の考えでは、この動作の理由は、Bashが以前の履歴行の変更を処理する方法によるものだと思います。previous-historyC- p)またはreverse-search-history( - )Cのようなコマンドの機能はr次のとおりです。得る前の履歴項目:

previous-history (C-p)
       Fetch the previous command from the history list, moving back in the list.

以前の履歴項目インポート入力したように印刷されます。これで、変更を加えて実行しないようにしたり(例のように)変更して実行したりできます。実行すると、以下を呼び出します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.

押すとReturn、変更された行が履歴に保存され、元の行は変更されません。しかし、インポートした行を押さずに修正するとどうなるか考えてみましょう。Return変更は行われたが実行されないため、accept-line呼び出されません。元の歴史的路線は修正済み

実際にこれを確認するには、次の行を追加して~/.inputrc新しいサブシェルを起動します。

set mark-modified-lines On

それでは、例と同じことをしましょう。

$ 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

質問の2番目の注釈からわかるように、2)でReturnキーを押すと、ポイント0)に入力された元のコマンドは変更せずにC-と呼び出すことができますr

さて、一見すると混乱して理解するのが難しいかもしれません。ご不明な点がございましたら、またお越しいただき、より明確に説明させていただきます。

おすすめ記事