bash-概述-脚本开头#!/bin/bash和#!/bin/sh的含义及区别

一直以为在shell脚本中#都是代表着注释功能,同样在脚本开始的#!/bin/sh也只是告诉用户这是一个shell脚本,而最近顺手查了下,才发现不是这个意思,分享下面的文章。

一、意思

#!/bin/sh是指此脚本使用/bin/sh来解释执行,#!是特殊的表示符,其后面跟的是此解释此脚本的shell的路径。

$ cat /etc/shells可以查看系统支持的shell格式

其实第一句的#!是对脚本的解释器程序路径,脚本的内容是由解释器解释的,我们可以用各种各样的解释器来写对应的脚本。

比如说/bin/csh脚本,/bin/perl脚本,/bin/awk脚本,/bin/sed脚本,甚至/bin/echo等等。

#!/bin/bash同理。

二、区别

Unix 系统是 Bourne Shell,Linux 系统是 Bourne Shell 的扩展即Bourne-Again Shell(bash)

GNU/Linux操作系统中的/bin/sh本是bash (Bourne-Again Shell) 的符号链接,但鉴于bash过于复杂,有人把bash从NetBSD移植到Linux并更名为dash (Debian Almquist Shell),并建议将/bin/sh指向它,以获得更快的脚本执行速度。Dash Shell 比Bash Shell小的多,符合POSIX标准。

所以也就是在Ubuntu中可以认为/bin/sh就是/bin/dash, 如果打算使用bash, 可直接将/bin/sh软链接到/bin/bash.

Ubuntu继承了Debian,所以从Ubuntu 6.10开始默认是Dash Shell。

应该说,/bin/sh/bin/bash虽然大体上没什么区别,但仍存在不同的标准。标记为#!/bin/sh的脚本不应使用任何POSIX没有规定的特性 (如let等命令, 但#!/bin/bash可以)。Debian曾经采用/bin/bash更改/bin/dash,目的使用更少的磁盘空间、提供较少的功能、获取更快的速度。但是后来经过shell脚本测试存在运行问题。因为原先在bash shell下可以运行的shell script (shell 脚本),在/bin/sh下还是会出现一些意想不到的问题,不是100%的兼用。

上面可以这样理解,使用man sh命令和man bash命令去观察,可以发现sh本身就是dash,也就更好的说明集成Debian系统之后的更改。

三、/bin/sh与/bin/bash的细微区别

在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本。
目前研发送测的shell脚本中主要有以下两种方式:
(1) #!/bin/sh
(2) #!/bin/bash
在这里求教同福客栈的各位大侠们一个问题:
以上两种方式有什么区别?对于脚本的实际运行会产生什么不同的影响吗?

脚本test.sh内容:
#!/bin/sh
source pcy.sh #pcy.sh并不存在
echo hello
执行./test.sh,屏幕输出为:
./test.sh: line 2: pcy.sh: No such file or directory
由此可见,在#!/bin/sh的情况下,source不成功,不会运行source后面的代码。//前提sh未设成bash的软链接

修改test.sh脚本的第一行,变为#!/bin/bash,再次执行./test.sh,屏幕输出为:
./test.sh: line 2: pcy.sh: No such file or directory
hello
由此可见,在#!/bin/bash的情况下,虽然source不成功,但是还是运行了source后面的echo语句。

但是紧接着我又试着运行了一下sh ./test.sh,这次屏幕输出为:
./test.sh: line 2: pcy.sh: No such file or directory
表示虽然脚本中指定了#!/bin/bash,但是如果使用sh 方式运行,如果source不成功,也不会运行source后面的代码。

为什么会有这样的区别呢?

junru同学作了解释

1. sh一般设成bash的软链
[work@zjm-testing-app46 cy]$ ll /bin/sh
lrwxrwxrwx 1 root root 4 Nov 13 2006 /bin/sh -> bash
2. 在一般的linux系统当中(如redhat),使用sh调用执行脚本相当于打开了bash的POSIX标准模式
3. 也就是说 /bin/sh 相当于 /bin/bash –posix

所以,sh跟bash的区别,实际上就是bash有没有开启posix模式的区别

so,可以预想的是,如果第一行写成 #!/bin/bash –posix,那么脚本执行效果跟#!/bin/sh是一样的(遵循posix的特定规范,有可能就包括这样的规范:“当某行代码出错时,不继续往下解释”)

例如:
[root@localhost yuhj]# head -n1 x.sh
#!/bin/sh
[root@localhost yuhj]# ./x.sh

./x.sh: line 8: syntax error near unexpected token `<‘
./x.sh: line 8: ` while read line; do { echo $line;((Lines++)); } ; done < <(route -n)’
[root@localhost yuhj]#

 

[root@localhost yuhj]# head -n1 x.sh
#!/bin/bash
[root@localhost yuhj]#./x.sh

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.202.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 192.168.202.2 0.0.0.0 UG 0 0 0 eth0
Number of lines read = 4
[root@localhost yuhj]#

[root@localhost yuhj]# head -n1 x.sh
#!/bin/bash –posix
[root@localhost yuhj]#
[root@localhost yuhj]# ./x.sh

./x.sh: line 8: syntax error near unexpected token `<‘
./x.sh: line 8: ` while read line; do { echo $line;((Lines++)); } ; done < <(route -n)’

 

[root@localhost yuhj]# whereis sh bash
sh: /bin/sh /usr/share/man/man1/sh.1.gz /usr/share/man/man1p/sh.1p.gz
bash: /bin/bash /usr/share/man/man1/bash.1.gz

[root@localhost yuhj]# ll /bin/sh /bin/bash
-rwxr-xr-x 1 root root 735004 May 25 2008 /bin/bash
lrwxrwxrwx 1 root root 4 Jan 29 00:39 /bin/sh -> bash

 

 

参考:

http://www.cnblogs.com/EasonJim/p/6850319.html
https://blog.csdn.net/MonMama/article/details/53390610

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments