语法
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