分享

Bash shell中bash、sh、source及“.”点等五种执行方式的区别与联系

 liang1234_ 2021-03-06

      版权声明:本文为耕耘实录原创文章,各大自媒体平台同步更新。欢迎转载,转载请注明出处,谢谢

在众多Linux发行版中bash shell 可谓是随处可见。作为众多发行版的首选shell,对于bash shell的学习对我们来说,显得格外重要。在学习bash shell的过程中,bash、sh、source及英文输入状态下的点号经常交替出现,他们看起来作用都差不多,但是深究下去,他们也有着不小的区别与联系。下面就让我们以具体实验来看一下它们之间的区别与联系吧!

一、查看当前系统支持的shell类型
[root@Geeklp-BashShell ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin

以上列出了当前系统所支持的shell类型。查看shell的历史我们可以知道,我们通常所说的bash shell(bash)全称为GNU Bourne-Again SHell。在目前的发行版中,sh已经成为bash的一个软连接。在man sh的时候大家都会发现,其实man出来的手册时bash的内容。

[root@Geeklp-BashShell ~]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 11月 20 10:05 /bin/sh -> bash
[root@Geeklp-BashShell ~]# ll /bin/bash
-rwxr-xr-x. 1 root root 960472 8月   3 05:11 /bin/bash
二、建立实验脚本

a.sh的内容如下:

#!/bin/bash - 
#===============================================================================
#          FILE: a.sh
#         USAGE: ./a.sh 
#        AUTHOR: Geeklp (IVAN DU), geeklp@qq.com
#  ORGANIZATION: GEEKLP
#       CREATED: 2018年01月19日 17时26分45秒
#      REVISION:  ---
#===============================================================================
set -o nounset
myName=`whoami`                              # Treat unset variables as an error
age=28
echo "My name is $myName .I am $age this year."

b.sh的内容如下:

#!/bin/bash - 
#===============================================================================
#          FILE: b.sh
#         USAGE: ./b.sh 
#        AUTHOR: Geeklp (IVAN DU), geeklp@qq.com
#  ORGANIZATION: GEEKLP
#       CREATED: 2018年01月19日 17时31分37秒
#      REVISION:  ---
#===============================================================================
set -o nounset                              # Treat unset variables as an error
echo "My name is $myName . I am $age this year."
三、具体探讨这几种执行方式的区别与联系

bash、sh分别执行a.sh和b.sh

[Geeklp@Geeklp-BashShell ~]$ bash a.sh 
My name is Geeklp .I am 28 this year.
[Geeklp@Geeklp-BashShell ~]$ bash b.sh 
b.sh:行11: myName: 为绑定变量
[Geeklp@Geeklp-BashShell ~]$ sh a.sh 
My name is Geeklp .I am 28 this year.
[Geeklp@Geeklp-BashShell ~]$ sh b.sh 
b.sh:行11: myName: 为绑定变量

可以看出来,bash及sh执行的脚本效果是一致的。 source及”.”分别执行a.sh、b.sh。

[Geeklp@Geeklp-BashShell ~]$ source a.sh 
My name is Geeklp .I am 28 this year.
[Geeklp@Geeklp-BashShell ~]$ source b.sh 
My name is Geeklp . I am 28 this year.
[Geeklp@Geeklp-BashShell ~]$ . ~/a.sh 
My name is Geeklp .I am 28 this year.
[Geeklp@Geeklp-BashShell ~]$ . ~/b.sh 
My name is Geeklp . I am 28 this year.

从上面的2个代码块中,我们可以看出,b.sh脚本直接引用了a.sh中的变了,如果我们依次执行几个有关联的脚本就可以采用这种方式,否则则使用bash及sh比较恰当。 注意:代码块2中的点与脚本路径中间有至少一个空格。 接下来,我们继续以点“.”来运行a.sh及b.sh。

[Geeklp@Geeklp-BashShell ~]$ ./a.sh
bash: ./a.sh: 权限不够

这个点与执行脚本之间无空格,表示执行当前目录下的a.sh脚本,相当于~/a.sh。b.sh也是一样的结果,无需讨论。

[Geeklp@Geeklp-BashShell ~]$ chmod u+x [ab].sh
[Geeklp@Geeklp-BashShell ~]$ ./a.sh 
My name is Geeklp .I am 28 this year.
[Geeklp@Geeklp-BashShell ~]$ ./b.sh 
./b.sh:行11: myName: 为绑定变量
[Geeklp@Geeklp-BashShell ~]$ ~/a.sh 
My name is Geeklp .I am 28 this year.
[Geeklp@Geeklp-BashShell ~]$ ~/b.sh 
/BashShell/b.sh:行11: myName: 为绑定变量

从以上代码块中不难看出:通过”.”(无空格)来执行脚本时需要给需要执行的脚本加上执行权限,否则无法执行。加了权限之后,执行效果与bash及sh一致,不继承变量。

四、结论
  1. bash与sh在对于脚本执行来说,效果一致,不继承除了当前shell之外的变量值。在无空格“.”(相对路径)、绝对路径且有执行权限的脚本执行中,效果与bash及sh一致。
  2. source及带空格的“.”执行效果一致,且会继承其他shell的变量。
  3. 除了不带空格的“.”之外,其他脚本的执行都不需要给脚本增加执行权限。
  4. bash、sh、不带空格的点(相对路径)或绝对路径中,脚本是在子进程中执行的。在source及带空格的点中,脚本是在父进程中执行的,差别很大。
五、一个小疑问

请大家看一下以下命令的效果:

[root@Geeklp-BashShell ~]# bash
[root@Geeklp-BashShell ~]# sh
sh-4.2# exit
exit
[root@Geeklp-BashShell ~]# bash --posix
bash-4.2# 

在上文中,我们能够看到sh是bash的一个软连接,但是单独输入bash和sh命令时,所呈现的效果却不一致?这里目前我并不是很清楚,麻烦有知道的小伙伴告知一下这是为什么?非常感谢!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多