Linux系统入门教程:如何知道当前正在使用的 Shell 是哪个?

问题: 我经常在命令行中切换 shell。是否有一个快速简便的方法来找出我当前正在使用的 shell 呢?此外,我怎么能找到当前 shell 的版本?

 

找到你当前正在使用的 Shell 版本

Linux系统入门教程:如何知道当前正在使用的 Shell 是哪个?

有多种方式可以查看你目前在使用什么 shell,最简单的方法就是通过使用 shell 的特殊参数。

其一,一个名为 "$$" 的特殊参数 表示当前你正在运行的 shell 实例的 PID。此参数是只读的,不能被修改。所以,下面的命令也将显示你正在运行的 shell 的名字:

  1. <span class="pln">$ </span><span class="kwd">ps</span><span class="pun">-</span><span class="pln">p $$</span>
  2. <span class="pln">PID TTY TIME CMD</span>
  3. <span class="lit">21666</span><span class="pln"> pts</span><span class="pun">/</span><span class="lit">4</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span><span class="kwd">bash</span>

上述命令可在所有可用的 shell 中工作。

如果你不使用 csh,找到当前使用的 shell 的另外一个办法是使用特殊参数 “$0” ,它表示当前正在运行的 shell 或 shell 脚本的名称。这是 Bash 的一个特殊参数,但也可用在其他 shell 中,如 sh、zsh、tcsh 或 dash。使用 echo 命令可以查看你目前正在使用的 shell 的名称。

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> $0</span>
  2. <span class="kwd">bash</span>

不要被一个叫做 $SHELL 的单独的环境变量所迷惑,它被设置为你的默认 shell 的完整路径。因此,这个变量并不一定指向你当前使用的 shell。例如,即使你在终端中调用不同的 shell,$SHELL 也保持不变。

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> $SHELL</span>
  2. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">shell</span>

Linux系统入门教程:如何知道当前正在使用的 Shell 是哪个?

因此,找出当前的shell,你应该使用 $$ 或 $0,但不是 $SHELL。

 

找出当前 Shell 的版本

一旦你知道你使用的是哪个 shell,你可能想知道此 shell 的版本。为此,在命令行中输入 shell 并在后面加上 “--version” 参数可以查看版本信息。例如:

对于 bash shell :

  1. <span class="pln">$ </span><span class="kwd">bash</span><span class="pun">--</span><span class="pln">version</span>
  2. <span class="pln">GNU </span><span class="kwd">bash</span><span class="pun">,</span><span class="pln"> version </span><span class="lit">4.3</span><span class="pun">.</span><span class="lit">30</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)-</span><span class="pln">release </span><span class="pun">(</span><span class="pln">x86_64</span><span class="pun">-</span><span class="pln">pc</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu</span><span class="pun">)</span>
  3. <span class="typ">Copyright</span><span class="pun">(</span><span class="pln">C</span><span class="pun">)</span><span class="lit">2013</span><span class="typ">Free</span><span class="typ">Software</span><span class="typ">Foundation</span><span class="pun">,</span><span class="typ">Inc</span><span class="pun">.</span>
  4. <span class="typ">License</span><span class="typ">GPLv3</span><span class="pun">+:</span><span class="pln"> GNU GPL version </span><span class="lit">3</span><span class="kwd">or</span><span class="pln"> later </span>
  5. <span class="typ">This</span><span class="kwd">is</span><span class="pln"> free software</span><span class="pun">;</span><span class="pln"> you are free to change </span><span class="kwd">and</span><span class="pln"> redistribute it</span><span class="pun">.</span>
  6. <span class="typ">There</span><span class="kwd">is</span><span class="pln"> NO WARRANTY</span><span class="pun">,</span><span class="pln"> to the extent permitted by law</span><span class="pun">.</span>

对于 zsh shell:

  1. <span class="pln">$ zsh </span><span class="pun">--</span><span class="pln">version</span>
  2. <span class="pln">zsh </span><span class="lit">5.0</span><span class="pun">.</span><span class="lit">7</span><span class="pun">(</span><span class="pln">x86_64</span><span class="pun">-</span><span class="pln">pc</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu</span><span class="pun">)</span>

对于 tcsh shell: $ tcsh --version

  1. <span class="pln">tcsh </span><span class="lit">6.18</span><span class="pun">.</span><span class="lit">01</span><span class="pun">(</span><span class="typ">Astron</span><span class="pun">)</span><span class="lit">2012</span><span class="pun">-</span><span class="lit">02</span><span class="pun">-</span><span class="lit">14</span><span class="pun">(</span><span class="pln">x86_64</span><span class="pun">-</span><span class="pln">unknown</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">)</span><span class="pln"> options wide</span><span class="pun">,</span><span class="pln">nls</span><span class="pun">,</span><span class="pln">dl</span><span class="pun">,</span><span class="pln">al</span><span class="pun">,</span><span class="pln">kan</span><span class="pun">,</span><span class="pln">rh</span><span class="pun">,</span><span class="pln">nd</span><span class="pun">,</span><span class="pln">color</span><span class="pun">,</span><span class="pln">filec</span>

对于某些 shell,你还可以使用 shell 特定的变量(例如,$BASHVERSION 或 $ZSHVERSION)。

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> $BASH_VERSION</span>
  2. <span class="lit">4.3</span><span class="pun">.</span><span class="lit">8</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)-</span><span class="pln">release</span>

via: http://ask.xmodulo.com/which-shell-am-i-using.html

作者:Dan Nanni 译者:strugglingyouth 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐