Bash 是我们经常与之打交道的 Shell 程序,本文针对其使用技巧进行了搜罗。相信在你看过这些内容之后,定会在 Bash 的世界里游刃有余。
mkdir /path/to/exampledir cd !$
本例中,第一行命令将创建一个目录,而第二行的命令则转到刚创建的目录。这里,“!$”的作用就是重复前一个命令的参数。事实上,不仅是命令的参数可以重复,命令的选项同样可以。另外,Esc
+ . 快捷键可以切换这些命令参数或选项。fg %3 又如: bg %7du -h -a -c $(find . -name *.conf 2>&-)
注意 $() 中的部分,这将告诉 Bash 运行 find 命令,然后把返回的结果作为 du 的参数。diff <(ps axo comm) <(ssh user@host ps axo
comm) 该命令将比较本地系统和远程系统中正在运行的进程。请注意 <() 中的部分。find . -name *.conf -print0 | xargs -0 grep -l -Z
mem_limit | xargs -0 -i cp {} {}.bak 该命令将备份当前目录中的所有 .conf 文件。ps aux | grep init 这里,“|”操作符将 ps aux
的输出重定向给 grep init。 下面还有两个稍微复杂点的例子: ps aux | tee filename | grep
init 及: ps aux | tee -a filename | grep initps aux > filename
注意其中的“>”符号。 你也可以将这些输出内容追加到一个已存在的文件中: ps aux >> filename
你还可以分割一个较长的行: command1 | command2 | ... | commandN > tempfile1
cat tempfile1 | command1 | command2 | ... | commandN >
tempfile2ps aux 2>&1 | grep init 这里的数字代表:
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
这是我所见过的 Bash 提示当中非常 Cool 的几个,使用它们能够让你充分地享受到 CLI 的高效,并免除重复输入的麻烦,从而节省大量地时间。
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
下面两个诀窍可以增强 Bash 的功能,一个是针对 Bash 的命令历史管理进行了改善,另一个是使 Bash 能够具有更加智能的自动完成特性。实现的过程并不复杂,只需修改 Bash 的默认配置即可。
~/.bashrc 或
~/.bash_profile 文件中: shopt -s histappend
PROMPT_COMMAND=‘history -a‘ 第一句的作用是将命令追加到 history 中。第二句是在显示命令提示符时,保存
history。.inputrc(如果该文件不存在,则创建一个)中加入下列内容: "\e[A":
history-search-backward "\e[B": history-search-forward set
show-all-if-ambiguous on 前两句使用 Up 和 Down 在 history 中进行搜索。后一句是按 Tab
显示自动完成。如果结合 Ctrl - R,则更加好用。××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
原文:http://www.cnblogs.com/samhugh/p/3613458.html