Shell script fails: Syntax error: "(" unexpected
The script does not begin with a shebang line, so the system executes it with /bin/sh
. On Ubuntu, /bin/sh
is dash, a shell designed for fast startup and execution with only standard features. When dash reaches line 68, it sees a syntax error: that parenthesis doesn't mean anything to it in context.
Since dash (like all other shells) is an interpreter, it won't complain until the execution reaches the problematic line. So even if the script successfully started at some point in your testing, it would have aborted once line 68 was reached.
The shebang line must be the very first thing in the file. Since you use bash features, the first line of the file must be #!/bin/bash
or #!/usr/bin/env bash
.
If the shebang is not on the first line, it will not be respected, regardless of the shell of the root user, the SHELL
variable or the -s
flag. You can easily confirm this is with a simple example:
#
#!/bin/bash
offfset=(`ls`)
echo $offset
Running this script with sudo will raise a syntax error in recent versions of Ubuntu and Debian.
You have two options to make sure the script is interpreted by bash
:
Move the shebang to the first line
Run
sudo
like this:sudo bash ./pi_dev_env_install.sh
For me starting script with:
bash ./< script file >
works fine.