17

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
2
  • Re "this function is working properly": Don't you mean "this function is not working properly" (my emphasis)? Commented Nov 27, 2021 at 23:25
  • 1
    OK, the OP left the building more than 10 years ago. Commented Nov 27, 2021 at 23:26

4 Answers 4

36

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! It makes sense now. I saw some websites that did that.
@rsmith Name the site or it was a dream :-)
See comments in stackoverflow.com/questions/7917018/… The "function" keyword is non-POSIX compliant, see "Shell Function Definitions" in the bash man page
Note 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/…
Does this depend on the version of Bash? Bash 4.1.2 on my system is perfectly fine with function MoveToTarget() { ....
|
5

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.

1 Comment

I'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.
5

If you encounter "Syntax error: "(" unexpected", then use "bash" instead of using "sh".

For example:

bash install.sh 

4 Comments

That will not help if your script is called by something else, e.g. a Git hooks pre-commit script.
Alternative: Get the information from which bash and use, e.g., #!/usr/bin/bash instead of #!/bin/sh.
#!/bin/bash may also work (as /bin may just point to /bin/usr).
/bin/sh may point to Dash (dash) - observed on Ubuntu MATE 20.04 (Focal Fossa).
0

I had the same issue. I was running scripts on Ubuntu sometimes using sh vs. Dash. It seems running scripts with sh causes the issue, but running scripts with Dash works fine.

1 Comment

Why is it different? Doesn't /bin/sh point to dash (symbolic link)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.