(1)命令功能
history命令用于显示指定数目的指令命令,读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入历史命令文件。
说明:bash启动的时候会读取~/.bash_history文件并载入到内存中,这个变量就用于设置.bash_history文件,bash退出时也会把内存中的历史回写到.bash_history文件,即history中显示的命令只保存在当前历史命令缓冲区中,只有退出登陆后系统才会把缓冲区的内容写入到历史命令文件中,当然也可以使用下面的命令参数实现。
(2)命令语法
history(选项) (参数)
(3)选项说明
-
-c:清空当前历史命令;
-
-d <行号>:清除指定行号的历史输入命令;
-
-a:将历史命令缓冲区中的命令写入历史命令文件中;
-
-r:将历史命令文件中的命令读入当前历史命令缓冲区;
-
-w:将当前历史命令缓冲区命令写入历史命令文件中。
(4)参数说明
-
n:打印最近的n条历史命令。
(5)实例
实例1:显示历史命令—history 数字
1
2
3
4
5
6
7
8
9
10
11
|
[root@moban ~]#history
10
991
alias
992
alias -p
993
man cd
994
unalias network
995
type network
996
alias network=
'cat /etc/sysconfig/network-scripts/ifcfg-eth0'
997
unalias network
998
type network
999
man history
1000
history
10
|
实例2:清除指定行号历史输入命令—history -d 行号
1
2
3
4
5
6
7
|
[root@moban ~]#history -d
1000
[root@moban ~]#history
5
997
unalias network
998
type network
999
man history
1000
history -d
1000
1001
history
5
|
可以看到实例1中的历史输入命令“1000 history 10”已被清除。
实例3:执行指定行号的历史输入命令--! 行号
1
2
3
4
5
6
7
8
9
|
[root@moban ~]#history
5
999
man history
1000
history -d
1000
1001
history
5
1002
pwd
1003
history
5
[root@moban ~]#!
1002
pwd
/root
|
实例4:运行上一个输入命令— !!
1
2
3
4
5
|
[root@moban ~]# pwd
/root
[root@moban ~]# !!
pwd
/root
|
实例5:将历史命令缓冲区中的命令写入历史命令文件中—history -a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@moban ~]# tail -
10
.bash_history
cat .bash_history
more .bash_history
history -
5
history
5
pwd
history
5
pwd
ls -a
sz .bash_history
init
6
[root@moban ~]#history -a
[root@moban ~]# tail -
10
.bash_history
pwd
ls -a
sz .bash_history
init
6
history
3
history
10
sy -y .bash_history
sz -y .bash_history
tail -
10
.bash_history
history -a
|
本文转自 xpleaf 51CTO博客,原文链接:http://blog.51cto.com/xpleaf/1671108,如需转载请自行联系原作者