Linux Shell教程(二) 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

历史命令:history

bash 有提供指令历史的服务,用命令history就可以查询我们曾经下达过的指令。

当然,如果觉得 histsory 要输入的字符太多太麻烦,可以使用命令别名来设定呢!

$ alias h='history'

如此则输入 h 等于输入 history 啦!

这是 history 命令的用法:

$ history [n]
$ history [-c]
$ history [-raw] histfiles
参数:
n :数字,意思是『要列出最近的 n 笔命令列表』的意思!
-c :将目前的 shell 中的所有 history 内容全部消除
-a :将目前新增的 history 指令新增入 histfiles 中,若没有加 histfiles ,
则预设写入 ~/.bash_history
-r :将 histfiles 的内容读到目前这个 shell 的 history 记忆中;
-w :将目前的 history 记忆内容写入 histfiles 中!

范例1:列出目前内存内的所有 history 记忆

$ history
# 前面省略
1017 man bash
1018 ll
1019 history
1020 history

列出的信息当中,共分两栏,第一栏为该指令在这个 shell 当中的代码,另一个则是指令本身的内容。至于会输出几笔指令记录,则与系统变量 HISTSIZE 有关。

范例2:列出最近使用的 3 个命令

$ history 3
1019 history
1020 history
1021 history 3

那么 history 这个历史命令只可以让我查询命令而已吗?当然不止啊!我们可以利用相关的功能来帮我们执行命令呢!

范例3:快速执行某个历史命令

$ history
66 man rm
67 alias
68 man history
69 history
$ !66 <==执行第 66 笔指令
$ !! <==执行上一个指令,本例中亦即 !66
$ !al <==执行最近以 al 为开头的指令(上头列出的第 67 个)
按照3个范例的做法执行history命令,查看结果。