Bash 的历史悠久,是一个古老的 Shell ,并且它还有一个更古老的前身 the Bourne Shell (sh) 。因此,Bash 的 history 命令是所有的 Linux Shell history 命令中功能最丰富的。Bash 版本的 history命令不仅支持反向搜索、快速调用,还支持重写历史记录等等功能。
善用 Bash history 命令以上的这些功能都可以提高你的工作效率,因此,让良许为你一一讲解 Bash history 命令以及它常用的功能。
history 是内置的命令
history 命令与许多其他的命令不同。你可能习惯于命令都作为可执行文件放置在常见的系统级的位置,例如 /usr/bin,/usr/local/bin 或 〜/ bin。但是,内置的 history 命令并不在你的环境变量 PATH 保存的路径中的。
实际上,history 命令并没有保存在物理位置中:
$ which history
which: no history in [PATH]
history 其实是 Shell 本身的一个内置函数:
$ type history
history is a shell builtin
$ help history
history: history [-c] [-d offset] [n] or
history -anrw [filename] or
history -ps arg [arg…]
Display or manipulate the history list.
[…]
由于 history 是 Shell 的内置函数,所以每种 Shell 的 history 函数都是独一无二的。因此,你在 Bash 中能使用的功能可能无法在 Tcsh,Fish 或 Dash 中使用,同样的,在 Tcsh,Fish 或 Dash 中能使用的功能也可能无法在 Bash 中使用。
查看你的 Bash 命令历史记录
history 命令最基本,最频繁的用法就是查看你的 Shell 会话的命令历史记录:
$ echo "hello"
hello
$ echo "world"
world
$ history
1 echo "hello"
2 echo "world"
3 history