Shell入门:掌握Linux,OS X,Unix的Shell环境
在Linux或类Unix系统中,每个用户和进程都运行在一个特定环境中。这个环境包含了变量、设置、别名、函数以及更多的东西。下面是对Shell环境下一些常用命令的简单介绍,包括每个命令如何使用的例子,以及在命令行下设定你自己的环境来提高效率。
找出你当前的shell
在终端应用中输入下面命令中的任意一个:
<span class="pln">ps $$</span>
<span class="pln">ps </span><span class="pun">-</span><span class="pln">p $$</span>
或者
<span class="pln">echo </span><span class="str">"$0"</span>
输出范例:
图1:找出当前的shell
找出所有已安装的shell
找到已安装shell的完整路径:
<span class="pln">type </span><span class="pun">-</span><span class="pln">a zsh</span>
<span class="pln">type </span><span class="pun">-</span><span class="pln">a ksh</span>
<span class="pln">type </span><span class="pun">-</span><span class="pln">a sh</span>
<span class="pln">type </span><span class="pun">-</span><span class="pln">a bash</span>
输出范例:
图2:找出shell的路径
文件/etc/shells里包含了系统所支持的shell列表。每一行代表一个shell,是相对根目录的完整路径。用这个cat命令来查看这些数据:
<span class="pln">cat </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">shells</span>
输出范例:
<span class="com"># List of acceptable shells for chpass(1).</span>
<span class="com"># Ftpd will not allow users to connect who are not using</span>
<span class="com"># one of these shells.</span>
<span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">bash</span>
<span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">csh</span>
<span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">ksh</span>
<span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">sh</span>
<span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">tcsh</span>
<span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">zsh</span>
<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:
<span class="pln">zsh</span>
这只是临时改变了系统shell。也叫做子shell。要从子/临时shell退出,输入下面的命令或者按下CTRL-D:
<span class="kwd">exit</span>
找出子shell的层级或临时shell的嵌套层级
每个bash实例启动后,变量$SHLVL的值都会加一。输入下面的命令:
<span class="pln">echo </span><span class="str">"$SHLVL"</span>
示例输出:
图3:Bash shell嵌套层级(子shell数目)
通过chsh命令永久变更系统shell
想要把当前系统shell从bash永久换成zsh?试试这个:
<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?试试这个:
<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>
相关推荐
firefaith 2020-10-30
以梦为马不负韶华 2020-10-20
wangzhaotongalex 2020-09-22
jarrygao 2020-11-02
woaimeinuo 2020-10-21
laisean 2020-09-01
libao 2020-09-16
卖口粥湛蓝的天空 2020-09-15
tianhuak 2020-11-24
huha 2020-10-16
lianshaohua 2020-09-23
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
CARBON 2020-10-20
彼岸随笔 2020-10-20
lianshaohua 2020-10-20