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

Bourne   shell是UNIX最初使用的shell,并且在每种UNIX上都可以使用。Bourne   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 过于复杂,有人把 ash 从 NetBSD 移植到 Linux 并更名为 dash(Debian Almquist Shell),并建议将 /bin/sh 指向它,以获得更快的脚本执行速度。Ubuntu 号称自从他们在 6.10 版里这样做了以后,系统启动速度有了明显的提升。Debian 计划在下一个发行版(代号 lenny)中也将 dash 作为默认的 /bin/sh。

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

在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]#

bash、dash(/bin/bash和/bin/sh)的区别的更多相关文章

  1. bin/bash 和 /bin/sh 的区别

    今天在用ssh Secure shell 连接虚拟机中的Ubuntu编写程序时,想比对一下两个源代码有什么差别,但是在一个ssh 客户端下不断的切换很是费劲.于是想着在主机中再添加一个用户.我原本用s ...

  2. Linux:/bin/bash和/bin/sh的区别

    bash.dash(/bin/bash和/bin/sh) 原文:http://www.cnblogs.com/dkblog/archive/2011/04/02/2003822.html Linux中 ...

  3. /bin/sh 与 /bin/bash 的区别

    /bin/sh 与 /bin/bash 的区别,用 : 截取字符串不是POSIX 标准的. 区别 sh 一般设成 bash 的软链 (symlink) ls -l /bin/sh lrwxrwxrwx ...

  4. [shell]Linux脚本开头#!/bin/bash和#!/bin/sh是什么意思以及区别

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

  5. #!/bin/sh与#!/bin/bash的区别

    Linux 中的 shell 有很多类型,其中最常用的几种是: Bourne shell (sh).C shell (csh) 和 Korn shell (ksh), 各有优缺点.Bourne she ...

  6. Linux脚本开头#!/bin/bash和#!/bin/sh是什么意思以及区别

    一.意思 #!/bin/sh是指此脚本使用/bin/sh来解释执行,#!是特殊的表示符,其后面根的是此解释此脚本的shell的路径. 其实第一句的#!是对脚本的解释器程序路径,脚本的内容是由解释器解释 ...

  7. -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

    刚刚学习SHELL 写了一个简单的例子发生如下错误 -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory ...

  8. 【问题解决】syntax error: unexpected end of file或-bash: ./full_build.sh: /bin/bash^M: bad interpreter: No

    在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 运行一个脚本full_build.sh 时, 一直是提示我: -bash: ./ ...

  9. Shell脚本报错:-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory

    在学习shell中测试case参数命令代码如下 #!/bin/bash #switch测试 case $1 in     start)         echo 'start'     ;;      ...

随机推荐

  1. css3自定义流动条

    <style> .item { height: 180px; overflow: auto; width: 180px; float: left; margin: 11px; box-sh ...

  2. P1165 日志分析 洛谷

    https://www.luogu.org/problem/show?pid=1165 题目描述 M 海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况 ...

  3. FJNUOJ1158(莫比乌斯反演)

    题目:给定n个数字a1...an.有m个询问,格式为L R X Y,意为求aL到aR之间与x的最大公因数为y的个数. 数据组数T<=20 1<=n,m<=1e5 1<=ai&l ...

  4. js转xml时 将xml中不需要的字符替换掉的方法replace()

    js中 replace(/\//g, '') 什么作用. 正则表达式 replace(/\//g, '') 的作用是把/替换成''. 用法如下: 比如:var aa= "adsdd/sdsd ...

  5. 【智能家居篇】wifi网络结构(下)

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢. 因为WIFI网络具有移动性,同一时候WIFI以无线电波作为传输媒介,这样的媒介本质上是开放的,且easy被拦截,不论 ...

  6. Python开发【第*篇】【模块】

    模块分为三种: 自定义模块 第三方模块 内置模块 1.模块导入 import model from model.xx.xx import xx from model.xx.xx import xx a ...

  7. make常见报错原因分析

    1 No rule to make target ‘xxx’ 原因一般是'xxx'文件不存在,首先应该去相应的目录检查文件是否存在.

  8. node.js 在函数内获取当前函数

    js 如何在函数体内部知道 自己在哪个函数内运行呢? 打比方: function a() { // 我想在这里知道我的函数名a和函数function a () {...} } 使用Error的调用栈可 ...

  9. kentico7中设置site的default page

    在SiteManager中,Settings中,选择好站点,然后Content,Default alias path

  10. pycharm打开多个项目并存

    问题: 有时我们需要打开多个项目,而现在的做法是:  原有的a项目不动,新打开一个pycharm来打开b项目, 或者 在原有的a项目中打开b项目并覆盖a项目,即a项目与b项目不能共存 需求: 有时我们 ...