shell文件无法用sh执行, sh无法执行数组, `Syntax error: “(” unexpected` when creating an array

Ubuntu bash functions..syntax error: “(” or “}” unexpected

如果用 sh 不执行一个shell文件,可以尝试使用 bash 执行

# 不通过
sh init.sh

# 尝试
bash init.sh

如果bash可行,那么为了使sh也可以使用,你需要修改默认的dash方式,如下图:

dpkg-reconfigure dash

如图,设置为 NO 即可,这样的话,取消了 sh 作为默认的 dash,那么,当你使用 sh 命令的时候,其实就是执行了 bash 命令。

 

 

 

拓展:

Linux 中的 shell 有很多类型,其中最常用的几种是: Bourne shell (sh)、C shell (csh) 和 Korn shell (ksh), 各有优缺点。Bourne shell 是 UNIX 最初使用的 shell,并且在每种 UNIX 上都可以使用, 在 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 指向它,以获得更快的脚本执行速度。Dash Shell 比 Bash Shell 小的多,符合POSIX标准。

 

更多参考:Ubuntu:shell脚本运行异常, bash和dash的区别, sh和bash的区别, bash、dash与zsh shell, source、sh、bash、./执行脚本的区别

 

本文:shell文件无法用sh执行, sh无法执行数组, `Syntax error: “(” unexpected` when creating an array

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.