巧妙使用Bash history 命令

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 命令并没有保存在物理位置中:


  1. $ which history 
  2.  
  3. which: no history in [PATH] 

history 其实是 Shell 本身的一个内置函数:


  1. $ type history 
  2. history is a shell builtin 
  3. $ help history 
  4. history: history [-c] [-d offset] [n] or 
  5. history -anrw [filename] or 
  6. history -ps arg [arg…] 
  7.  
  8. Display or manipulate the history list. 
  9. […] 

由于 history 是 Shell 的内置函数,所以每种 Shell 的 history 函数都是独一无二的。因此,你在 Bash 中能使用的功能可能无法在 Tcsh,Fish 或 Dash 中使用,同样的,在 Tcsh,Fish 或 Dash 中能使用的功能也可能无法在 Bash 中使用。

查看你的 Bash 命令历史记录

history 命令最基本,最频繁的用法就是查看你的 Shell 会话的命令历史记录:


  1. $ echo "hello" 
  2. hello 
  3. $ echo "world" 
  4. world 
  5. $ history 
  6.   1  echo "hello" 
  7.   2  echo "world" 
  8.   3  history 
【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章