<div class="content-detail markdown-body">
<h1 style="font-size:32px;font-family:'Microsoft YaHei', 'Hiragino Sans GB', STHeiti, Tahoma, SimHei, sans-serif;font-weight:100;text-align:center;">Linux 有问必答:如何知道当前正在使用的 shell 是哪个?</h1>
<div>
<div>
<blockquote style="width:634.391px;font-family:'Lantinghei SC', 'Helvetica Neue', 'Microsoft YaHei', '微软雅黑', Arial, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif;font-size:16px;line-height:32px;">
<p style="line-height:2em;"><span style="color:rgb(196,15,15);">问题</span>: 我经常在命令行中切换 shell。是否有一个快速简便的方法来找出我当前正在使用的 shell 呢?此外,我怎么能找到当前 shell 的版本?</p>
</blockquote>
<a class="target-fix" style="color:rgb(247,110,110);border-bottom-style:solid;border-bottom-width:2px;border-bottom-color:rgb(238,238,238);font-family:'Lantinghei SC', 'Helvetica Neue', 'Microsoft YaHei', '微软雅黑', Arial, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif;font-size:16px;line-height:32px;border-top-style:none;border-right-style:none;border-left-style:none;"></a>
<h3 style="font-size:1.4em;font-family:'Microsoft YaHei', 'Hiragino Sans GB', STHeiti, Tahoma, SimHei, sans-serif;font-weight:300;color:rgb(255,255,255);line-height:1.8em;letter-spacing:3px;border-left-style:solid;border-left-color:rgb(51,51,51);border-left-width:10px;">找到你当前正在使用的 Shell 版本</h3>
<p style="line-height:32px;font-family:'Lantinghei SC', 'Helvetica Neue', 'Microsoft YaHei', '微软雅黑', Arial, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif;font-size:16px;"><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-6d4037a9a5dc5217dc5fceadd5e81ef2.jpg" style="text-align:center;border:none;"></p>
<p style="line-height:32px;font-family:'Lantinghei SC', 'Helvetica Neue', 'Microsoft YaHei', '微软雅黑', Arial, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif;font-size:16px;">有多种方式可以查看你目前在使用什么 shell,最简单的方法就是通过使用 shell 的特殊参数。</p>
<p style="line-height:32px;font-family:'Lantinghei SC', 'Helvetica Neue', 'Microsoft YaHei', '微软雅黑', Arial, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif;font-size:16px;">其一,<a href="http://ask.xmodulo.com/process-id-pid-shell-script.html" style="color:rgb(247,110,110);text-decoration:none;border-bottom-width:2px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);">一个名为 "$$" 的特殊参数</a> 表示当前你正在运行的 shell 实例的 PID。此参数是只读的,不能被修改。所以,下面的命令也将显示你正在运行的 shell 的名字:</p>
<pre class="prettyprint linenums prettyprinted"></pre>
<ol style="border-left-width:2px;border-left-style:solid;border-left-color:rgb(0,153,0);color:rgb(174,174,174);"><li class="L0" style="text-indent:-28px;line-height:2em;list-style-type:decimal;"><code style="color:rgb(184,255,184);border:none;background:none;"><span class="pln">$ </span><span class="kwd" style="color:rgb(226,137,100);">ps</span><span class="pln"> </span><span class="pun">-</span><span class="pln">p $$</span></code></li><li class="L1" style="text-indent:-28px;line-height:2em;list-style-type:decimal;"><code style="color:rgb(184,255,184);border:none;background:none;"></code></li><li class="L2" style="text-indent:-28px;line-height:2em;list-style-type:decimal;"><code style="color:rgb(184,255,184);border:none;background:none;"><span class="pln"> PID TTY TIME CMD</span></code></li><li class="L3" style="text-indent:-28px;line-height:2em;list-style-type:decimal;"><code style="color:rgb(184,255,184);border:none;background:none;"><span class="lit" style="color:rgb(51,135,204);">21666</span><span class="pln"> pts</span><span class="pun">/</span><span class="lit" style="color:rgb(51,135,204);">4</span><span class="pln"> </span><span class="lit" style="color:rgb(51,135,204);">00</span><span class="pun">:</span><span class="lit" style="color:rgb(51,135,204);">00</span><span class="pun">:</span><span class="lit" style="color:rgb(51,135,204);">00</span><span class="pln"> </span><span class="kwd" style="color:rgb(226,137,100);">bash</span></code></li></ol>
<p style="line-height:32px;font-family:'Lantinghei SC', 'Helvetica Neue', 'Microsoft YaHei', '微软雅黑', Arial, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif;font-size:16px;">上述命令可在所有可用的 shell 中工作。</p>
<p style="line-height:32px;font-family:'Lantinghei SC', 'Helvetica Neue', 'Microsoft YaHei', '微软雅黑', Arial, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif;font-size:16px;">如果你不使用 csh,找到当前使用的 shell 的另外一个办法是使用特殊参数 “$0” ,它表示当前正在运行的 shell 或 shell 脚本的名称。这是 Bash 的一个特殊参数,但也可用在其他 shell 中,如 sh、zsh、tcsh 或 dash。使用 echo 命令可以查看你目前正 |
|