Bash

如何合併終端上重複的命令歷史記錄?

  • December 20, 2020

你有過這樣的經歷嗎?

當您嘗試在終端上查找 previos used 命令時up arrow,您會發現

$ls         # 1st time push `up arrow`
$ls         # 2nd time push `up arrow`
$ls         # 3rd time push `up arrow`
$ls         # 4th time push `up arrow`
$ls         # 5th time push `up arrow`
$ls         # 6th time push `up arrow`
$make       # 7th time push `up arrow`
$make       # 8th time push `up arrow`
$make       # 9th time push `up arrow`
$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"   # Bingo!

如果是這樣就更好了

$ls         # 1st time push `up arrow`
$make       # 2th time push `up arrow`
$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"   # Bingo!

因為重複的歷史通常沒有用。

任何的想法?

您可以通過ignoredups在 HISTCONTROL 環境變數中進行設置來完成此操作:

export HISTCONTROL="ignoredups"

從 bash 手冊頁:

   HISTCONTROL
          A  colon-separated list of  values controlling how commands are 
          saved on the history list. If the list of values includes ignorespace, 
          lines which begin with a space character are not saved in the  history 
          list.   A  value  of ignoredups  causes  lines  matching the previous 
          history entry to not be saved.  A value of ignoreboth is shorthand
          for ignorespace and ignoredups.  A value of erasedups causes all
          previous lines matching the  current  line  to  be removed  from the 
          history list before that line is saved.  Any value not in the above 
          list is ignored.  If HISTCONTROL is unset, or does not include a valid 
          value, all lines read by the shell parser are saved on the history 
          list, subject  to  the  value  of  HISTIGNORE.   The second and 
          subsequent lines of a multi-line compound command are not tested,
          and are added to the history regardless of the value of
          HISTCONTROL.

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