Bash - error message 'Syntax error: "(" unexpected'
Solution 1
You're using the wrong syntax to declare functions. Use this instead:
MoveToTarget() {
# Function
}
Or this:
function MoveToTarget {
# function
}
But not both.
Also, I see that later on you use commas to separate arguments (MoveToTarget $SourcePath, $DestPath
). That is also a problem. Bash uses spaces to separate arguments, not commas. Remove the comma and you should be golden.
Solution 2
I'm also new to defining functions in Bash scripts. I'm using a Bash of version 4.3.11(1):-release (x86_64-pc-linux-gnu) on Ubuntu 14.04 (Trusty Tahr).
I don't know why, but the definition that starts with the keyword function
never works for me.
A definition like the following
function check_and_start {
echo Hello
}
produces the error message:
Syntax error: "}" unexpected
If I put the {
on a new line like:
function my_function
{
echo Hello.
}
It prints a Hello.
when I run the script, even if I don't call this function at all, which is also what we want.
I don't know why this wouldn't work, because I also looked at many tutorials and they all put the open curly brace at the end of the first line. Maybe it's the version of Bash that we use?? Anyway, just put it here for your information.
I have to use the C-style function definition:
check_and_start() {
echo $1
}
check_and_start World!
check_and_start Hello,\ World!
and it works as expected.
Solution 3
If you encounter "Syntax error: "(" unexpected", then use "bash" instead of using "sh".
For example:
bash install.sh
rsmith
Updated on July 12, 2022Comments
-
rsmith almost 2 years
For some reason, this function is working properly. The terminal is outputting
newbootstrap.sh: 2: Syntax error: "(" unexpected
Here is my code (line 2 is function
MoveToTarget() {
)#!/bin/bash function MoveToTarget() { # This takes two arguments: source and target cp -r -f "$1" "$2" rm -r -f "$1" } function WaitForProcessToEnd() { # This takes one argument. The PID to wait for # Unlike the AutoIt version, this sleeps for one second while [ $(kill -0 "$1") ]; do sleep 1 done } function RunApplication() { # This takes one application, the path to the thing to execute exec "$1" } # Our main code block pid="$1" SourcePath="$2" DestPath="$3" ToExecute="$4" WaitForProcessToEnd $pid MoveToTarget $SourcePath, $DestPath RunApplication $ToExecute exit
-
rsmith almost 13 yearsThanks! It makes sense now. I saw some websites that did that.
-
Jens about 8 years@rsmith Name the site or it was a dream :-)
-
Jay Soyer almost 8 yearsI'm having the same exact issue. Dropping the word function and adding paran worked for me too. I noticed either function style will work fine on OS X. Though my OS X is running a much older version of bash.
-
qneill over 6 yearsSee comments in stackoverflow.com/questions/7917018/… The "function" keyword is non-POSIX compliant, see "Shell Function Definitions" in the bash man page
-
林果皞 over 6 yearsNote also empty function body is not allow which is the most confusing part (since someone may narrow down to test empty body but still not working), see stackoverflow.com/questions/39307615/…
-
林果皞 over 6 yearsAlso
sh
might symlink to /bin/dash and you need run it asbash
. -
ruakh about 5 yearsDoes this depend on the version of Bash? Bash 4.1.2 on my system is perfectly fine with
function MoveToTarget() { ...
. -
Peter Mortensen over 2 yearsThat will not help if your script is called by something else, e.g. a Git hooks pre-commit script.
-
Peter Mortensen over 2 yearsAlternative: Get the information from
which bash
and use, e.g.,#!/usr/bin/bash
instead of#!/bin/sh
. -
Peter Mortensen over 2 years
#!/bin/bash
may also work (as/bin
may just point to/bin/usr
). -
Peter Mortensen over 2 yearsWhy is it different? Doesn't
/bin/sh
point todash
(symbolic link)? -
Peter Mortensen over 2 years