有 shell 动态附加历史记录吗?

有 shell 动态附加历史记录吗?

是否有任何 shell 会动态附加历史记录,以便历史记录在崩溃时幸存下来?

答案1

zsh中有很多相关选项:

引用手册(参见info zsh appendhistory):

APPEND_HISTORY <D>
     If this is set, zsh sessions will append their history list to the
     history file, rather than replace it.  Thus, multiple parallel zsh
     sessions will all have the new entries from their history lists
     added to the history file, in the order that they exit.  The file
     will still be periodically re-written to trim it when the number of
     lines grows 20% beyond the value specified by $SAVEHIST (see also
     the HIST_SAVE_BY_COPY option).

INC_APPEND_HISTORY
     This option works like APPEND_HISTORY except that new history lines
     are added to the $HISTFILE incrementally (as soon as they are
     entered), rather than waiting until the shell exits.  The file will
     still be periodically re-written to trim it when the number of
     lines grows 20% beyond the value specified by $SAVEHIST (see also
     the HIST_SAVE_BY_COPY option).

INC_APPEND_HISTORY_TIME
     This option is a variant of INC_APPEND_HISTORY in which, where
     possible, the history entry is written out to the file after the
     command is finished, so that the time taken by the command is
     recorded correctly in the history file in EXTENDED_HISTORY format.
     This means that the history entry will not be available immediately
     from other instances of the shell that are using the same history
     file.

     This option is only useful if INC_APPEND_HISTORY and SHARE_HISTORY
     are turned off.  The three options should be considered mutually
     exclusive.

SHARE_HISTORY <K>

     This option both imports new commands from the history file, and
     also causes your typed commands to be appended to the history file
     (the latter is like specifying INC_APPEND_HISTORY, which should be
     turned off if this option is in effect).  The history lines are
     also output with timestamps ala EXTENDED_HISTORY (which makes it
     easier to find the spot where we left off reading the file after it
     gets re-written).

     By default, history movement commands visit the imported lines as
     well as the local lines, but you can toggle this on and off with
     the set-local-history zle binding.  It is also possible to create a
     zle widget that will make some commands ignore imported commands,
     and some include them.

     If you find that you want more control over when commands get
     imported, you may wish to turn SHARE_HISTORY off,
     INC_APPEND_HISTORY or INC_APPEND_HISTORY_TIME (see above) on, and
     then manually import commands whenever you need them using 'fc
     -RI'.

答案2

bash 可以,只需将 PROMPT_COMMAND 设置为(或包含)'history -a'。有关完整详细信息,请参阅“history”内置命令和“man bash”中的 PROMPT_COMMAND 变量。

相关内容