dash和bash

dash

通常使用sh作为shell脚本的执行命令,其位于

$ which sh
/bin/sh

其是一个链接,指向

$ file /bin/sh
/bin/sh: symbolic link to dash

所以dash是默认解释器

$ file /bin/dash
/bin/dash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=504637666875a5d526ef51acfe601c99efc99114, stripped

bash

在编写shell文件时,会在开头加上如下语句

#!/bin/bash

其含义是再没有指定解释器时,使用/bin/bash作为脚本解释器执行

$ file /bin/bash
/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=6f072e70e3e49380ff4d43cdde8178c24cf73daa, stripped

sh vs. dash vs. bash

参考:

Difference between sh and bash

Ubuntu dash与bash的区别

  • sh是一种编程语言,指的是shell命令语言,其规范由POSIX standard指定
  • bash(the GNU Bourne-Again Shell)dash(the Debian Almquist Shell)是兼容sh规范的脚本解释器
  • 对于DebianUbuntu系统而言,其默认的sh链接指向的是dash

bash能实现的功能比dash多,具体差别参考bash和dash区别,所以执行脚本时使用bash

$ bash test.sh

[[: not found

使用sh命令执行脚本test.sh

$ cat test.sh 
#!/bin/bash

NAME="asdf asdf as"

if [[ ${NAME} == as* ]]
then
    echo "匹配"
else
    echo "不匹配"
fi

得到问题如下:

$ sh test.sh 
test.sh: 5: test.sh: [[: not found
不匹配

原因是相比较于bash而言,dash的功能少,无法解析[[ ]],使用bash执行即可

$ bash test.sh 
匹配