Shell入门:掌握Linux,OS X,Unix的Shell环境

在Linux或类Unix系统中,每个用户和进程都运行在一个特定环境中。这个环境包含了变量、设置、别名、函数以及更多的东西。下面是对Shell环境下一些常用命令的简单介绍,包括每个命令如何使用的例子,以及在命令行下设定你自己的环境来提高效率。

Shell入门:掌握Linux,OS X,Unix的Shell环境

找出你当前的shell

在终端应用中输入下面命令中的任意一个:

  1. <span class="pln">ps $$</span>
  2. <span class="pln">ps </span><span class="pun">-</span><span class="pln">p $$</span>

或者

  1. <span class="pln">echo </span><span class="str">"$0"</span>

输出范例:

Shell入门:掌握Linux,OS X,Unix的Shell环境

图1:找出当前的shell

找出所有已安装的shell

找到已安装shell的完整路径:

  1. <span class="pln">type </span><span class="pun">-</span><span class="pln">a zsh</span>
  2. <span class="pln">type </span><span class="pun">-</span><span class="pln">a ksh</span>
  3. <span class="pln">type </span><span class="pun">-</span><span class="pln">a sh</span>
  4. <span class="pln">type </span><span class="pun">-</span><span class="pln">a bash</span>

输出范例:

Shell入门:掌握Linux,OS X,Unix的Shell环境

图2:找出shell的路径

文件/etc/shells里包含了系统所支持的shell列表。每一行代表一个shell,是相对根目录的完整路径。用这个cat命令来查看这些数据:

  1. <span class="pln">cat </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">shells</span>

输出范例:

  1. <span class="com"># List of acceptable shells for chpass(1).</span>
  2. <span class="com"># Ftpd will not allow users to connect who are not using</span>
  3. <span class="com"># one of these shells.</span>
  4. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">bash</span>
  5. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">csh</span>
  6. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">ksh</span>
  7. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">sh</span>
  8. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">tcsh</span>
  9. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">zsh</span>
  10. <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">fish</span>

临时改变当前shell

只需要输入shell的名字。在下面的例子里,我从bash切换到了zsh:

  1. <span class="pln">zsh</span>

这只是临时改变了系统shell。也叫做子shell。要从子/临时shell退出,输入下面的命令或者按下CTRL-D:

  1. <span class="kwd">exit</span>

找出子shell的层级或临时shell的嵌套层级

每个bash实例启动后,变量$SHLVL的值都会加一。输入下面的命令:

  1. <span class="pln">echo </span><span class="str">"$SHLVL"</span>

示例输出:

Shell入门:掌握Linux,OS X,Unix的Shell环境

图3:Bash shell嵌套层级(子shell数目)

通过chsh命令永久变更系统shell

想要把当前系统shell从bash永久换成zsh?试试这个:

  1. <span class="pln">chsh </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">zsh</span>

想把其他用户的shell从bash永久换成ksh?试试这个:

  1. <span class="pln">sudo chsh </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">ksh userNameHere</span>

相关推荐