語法
history [-c] [-d offset] [n] or history -anrw [filename]
常用選項
-c:clear the history list by deleting all of the entries. 清空history list。可以用 -r 選項再讀回來。
-d offset:delete the history entry at offset OFFSET. 刪除指定的命令歷史。
-a:append history lines from this session to the history file. 將新增的history追加到history file。
-r:read the history file and append the contents to the history list. 從history file讀取命令並追加到history list。
-n:read all history lines not already read from the history file.
-w:write the current history to the history file and append them to the history list. 將當前會話的history寫入history file。
常用技巧
顯示最近10條命令歷史
history 10
重復執行第N條命令
!n
重復執行倒數第N條命令
!-n
重復執行上一條命令
!!
重復執行最近一條以string開頭的命令
!string
調用上一條命令的最后一個參數
a、快捷鍵:ESC+.
b、字符串:!$
通過環境變量定制 history 的功能
HISTSIZE:history可保留的命令歷史的條數。如果這個值設置為0,則不記錄history。
HISTFILE:命令歷史文件位置。默認為 .bash_history,清空或者刪除該文件,可以清除命令歷史記錄。
HISTFILESIZE:命令歷史記錄條數。(.bash_history文件)
HISTTIMEFORMAT:顯示時間的格式
HISTCONTROL:控制命令歷史記錄,支持四種方式。
HISTCONTROL=ignoredups:忽略連續重復的命令。
HISTCONTROL=ignorespace:忽略以空白字符開頭的命令。
HISTCONTROL=ignoreboth:同時忽略以上兩種。
HISTCONTROL=erasedups:忽略所有歷史命令中的重復命令。
下面是我在mac上的配置,僅供參考
export HISTTIMEFORMAT="%F %T "
export HISTFILESIZE=100000
export HISTSIZE=100000
export HISTCONTROL=ignoreboth
效果如下圖
清空 history
當前會話執行的命令記錄,放置在緩存中,正常退出或關閉時,會把緩存信息寫入 ~/.bash_history。
當會話直接被kill時,緩存中的命令不會寫入 ~/.bash_history。
清空當前會話中的命令記錄(不影響本地歷史文件)
history -c
如果要徹底清空 history,需要清空歷史命令文件
> ~/.bash_history