history命令

导读:本篇文章讲解 history命令,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

history概述

Bash 有完善的历史命令,这对于简化管理操作、排查系统错误都有重要的作用,而且使用简单方便,建议大家多使用历史命令。系统保存的历史命令可以使用 history 命令查询。

命令使用

命令格式:
[root@localhost ~]# history [选项] [历史命令保存文件]
选项:
-c:清空历史命令;
-w:把缓存中的历史命令写入历史命令保存文件中。如果不手工指定历史命令保存文件,则放入默认历史命令保存文件 ~/.bash_history 中;
查看历史命令
[root@localhost test]# history 
    1  history 
    2  ll
    3  ls
    4  history 
[root@localhost test]# cat /etc/profile | grep HISTSIZE=  #查看历史默认保存条数可修改
HISTSIZE=1000   
#大家需要注意,每个用户的历史命令是单独保存的,所以每个用户的家目录中都有 .bash_history 这个历史命令文件。
#如果最多保存100,那么第101会被保存,第一条会被删除。
[root@localhost test]# history -w #把缓存中的历史命令直接写入~/.bash_history
[root@localhost test]# cat /root/.bash_history
...此处省略...
cat /etc/profile | grep HISTSIZE=
cat .bash_history 
history -w
[root@localhost test]# history -c
#清空历史命令

历史命令调用

  • 上下箭头翻看命令
  • 使用!n重复执行第n条命令
[root@localhost test]# history 
    1  history 
...此处省略...
   42  mv /root/test.sh .
   43  ll
   44  history 
[root@localhost test]# !43
ll
总用量 4
-rw-r--r-- 1 root root 31 223 11:33 test.sh
  • 使用!!重复执行上条命令
[root@localhost test]# ll
总用量 4
-rw-r--r-- 1 root root 31 223 11:33 test.sh
[root@localhost test]# !!
ll
总用量 4
-rw-r--r-- 1 root root 31 223 11:33 test.sh
  • 使用!字符串重复执行最后一条以该字符串开头的命令
[root@localhost test]# history 
    1  history 
...此处省略...
   46  history 
   47  ll
   48  history 
[root@localhost test]# !l
ll
总用量 4
-rw-r--r-- 1 root root 31 223 11:33 test.sh
  • 使用!$重复执行上一条命令最后一个参数
[root@localhost test]# cat test.sh 
#!/bin/bash
name=xy
echo $name
[root@localhost test]# cat !$
cat test.sh
#!/bin/bash
name=xy
echo $name

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/11968.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!