[:unexpected operator in shell script以及bash/sh/dash之间的关系

解决方法参考自https://stackoverflow.com/questions/3411048/unexpected-operator-in-shell-programming#以及https://wiki.ubuntu.com/DashAsBinSh

错误显示

鸟哥私房菜基础版第三版383页的脚本:
[:unexpected operator in shell script以及bash/sh/dash之间的关系
运行:sh sh06.sh出错
出错信息:[:unexpected operator

解决方式

解决方式:使用bash ./sh06.sh运行或者将脚本中的==改为=,使用sh sh06.sh运行
原因:

错误原因

Yes. They are completely different shells. Although, bash was based on and is largely backwards-compatable with sh, and they might actually be the same program on your system, but will still behave differently depending on which name you use. You can have the script run with bash automatically by changing the first line to #!/bin/bash and making the file executable, and just running ./choose.sh

POSIX sh doesn’t understand == for string equality, as that is a bash-ism. Use = instead.

bash/sh/dash

/bin/bash是一款很好的全功能/适合交互的shell,与/bin/dash相比,它大得多且速度慢。
dash提供的功能集主要是bash提供的功能集的子集(subset)
ubuntu中,/bin/sh符号连接到/bin/dash。默认的登录shell仍为bash。从菜单或快捷方式[crtl-alt-t]打开终端提供交互式bash。从桌面或文件管理器运行的脚本,通过对话框“在终端中运行”将作为POSIX dash执行,除非正确指定。

在参考地址的第二个地址中列出了一些常见的bash扩展(bashisms),本例子中的[命令就是常见的陷阱。