Linux

您鍵入的命令從歷史記錄中失去的情況有哪些?

  • September 18, 2017

我輸入了一堆命令,當我執行時,history我發現它們不存在。

除了命令以 a 開頭的情況 (space) what are the cases when commands will not be logged in history?

You've already mentioned the leading space, and another answer has mentioned settings that will *intentionally* change what is saved in the history such as the HISTIGNORE` setting and simply turning history off.

Another scenario is multiple interactive shells run by the same user. The Z shell has a share_history option that makes it possible to have more than one Z shell instance updating a shared history file. It re-reads the file looking for new entries, and it applies timestamps to each entry.

The Bourne Again shell does not have this built-in (albeit that one can sort of do so using shenanighans to execute commands when the prompt is printed). The default history file behaviour that this is altering is that neither shell expects anything other than itself to be writing to the history file, and does not update the history after every command line executed.

The default behaviour in the Bourne Again shell, specifically, only updates the history file when the shell exits or one explicitly tells it to with the history command. This means not only that one shell will simply overwrite the history written by another when a user has multiple interactive shell sessions; but also that it is possible for a shell that is not terminated cleanly to not write out the history file and thus to lose whatever command history occurred between the shell being uncleanly terminated and its last (explicit) update of the history file.

Further reading

引用自:https://unix.stackexchange.com/questions/392938