[shell] sh executes the script and reports an error syntax error: “(” unexpected “)

Question:

The SH script contains the contents of array initialization

$ str="123 456 789"
$ array=($str)
$ echo ${array[2]}

SH executing the script will report an error  Syntax error: "(" unexpected

reason:

Other common Linux distributions, although many point sh to bash

The SH command on Debian/Ubuntu points to dash instead of bash by default

And because dash is lighter than bash, it only supports basic shell functions,  

It does not include the array initialization just now, so it can not be recognized. Syntx error is directly reported

solve:

  The solution is to use it directly   bash test. Sh , or ./test. Sh , these two ways to execute the script.


When executing sh./xxx.sh, the solution of “syntax error:” (“unexpected”) appears

  Yesterday, the system identification part of virtualmin was updated to enable it to support one click installation and optimization under Debian system. The code was almost modified. Reinstall the VPS into Debian. Execute the code through sh./virtualmin.sh and report “syntax error:” (“unexpected”) Error. It’s OK to execute through bash./virtualmin.sh. After searching the syntax for several times, there’s no problem. Later, find the cause of the problem on the Internet:
        The code is correct for standard bash, because Ubuntu/Debian uses dash instead of traditional bash in order to speed up startup. Dash is making trouble. The solution is to cancel dash.

solve:

1) sudo dpkg-reconfigure dash         Select no from the options and it’s done!

2) Execute dpkg reconfigure dash under root and select No

zxl@ubuntu :~/package$ su
Password:
root@ubuntu :/home/zxl/package# dpkg-reconfigure dash


Read More: