bash、dash(/bin/bash和/bin/sh)的区别

Linux中的shell有多种类型,其中最经常使用的几种是Bourne   shell(sh)、C   shell(csh)和Korn   shell(ksh)。三种shell各有优缺点。linux

Bourne   shell是UNIX最初使用的shell,而且在每种UNIX上均可以使用。Bourne   shell在shell编程方面至关优秀,但在处理与用户的交互方面作得不如其余几种shell。shell

Linux操做系统缺省的shell是Bourne   Again   shell,它是Bourne   shell的扩展,简称Bash,与Bourne   shell彻底向后兼容,而且在Bourne   shell的基础上增长、加强了不少特性。Bash放在/bin/bash中,它有许多特点,能够提供如命令补全、命令编辑和命令历史表等功能,它还包含了不少C   shell和Korn   shell中的优势,有灵活和强大的编程接口,同时又有很友好的用户界面。编程

GNU/Linux 操做系统中的 /bin/sh 是 bash(Bourne-Again Shell)的符号连接,bash

    但鉴于 bash 过于复杂,有人把 ash 从 NetBSD 移植到 Linux 并改名为 dash(Debian Almquist Shell),并建议将 /bin/sh 指向它,以得到更快的脚本执行速度。Ubuntu 号称自从他们在 6.10 版里这样作了之后,系统启动速度有了明显的提高。Debian 计划在下一个发行版(代号 lenny)中也将 dash 做为默认的 /bin/sh。app

 

/bin/sh与/bin/bash的细微区别ui

在shell脚本的开头每每有一句话来定义使用哪一种sh解释器来解释脚本。
目前研发送测的shell脚本中主要有如下两种方式:
(1) #!/bin/sh
(2) #!/bin/bash


值得注意的是:
1. sh通常设成bash的软链
[work@zjm-testing-app46 cy]$ ll /bin/sh
lrwxrwxrwx 1 root root 4 Nov 13 2017 /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 Nov 25 2017/bin/bash
lrwxrwxrwx 1 root root 4 Jan 29 00:39 /bin/sh -> bash
[root@localhost yuhj]# 操作系统

相关文章
相关标签/搜索