shell script error : Syntax error: "(" unexpected

8,730

My guess is that you don't run this script as a Bash script, but with some other shell, which doesn't accept this syntax. Eg. sh.

As you can see in Shellcheck, and as Bash's man on my Debian confirms, this syntax is correct:

name () compound-command [redirection]
function name [()] compound-command [redirection]

You are using the second one from the two above, and your compound-command is the contents of the {...} braces. And that's fine as far as man bash goes. But then, there are voices against this construct, as in the link in John Moon's comment below the other answer. And in the link to Greg's Wiki in that answer again.

Going back to your question, taking into consideration the name of the script, namely Dockerfunctiontest.sh, I might think you don't see much difference between sh and bash. Probably you run the script with this command:

sh Dockerfunctiontest.sh

While it would be reasonable to run it with either:

bash Dockerfunctiontest.sh

or

./Dockerfunctiontest.sh

The last one picks the interpreter basing on the first line in the script, namely:

#!/bin/bash

And while the interpreter named in the script is Bash, why not name the file the proper way?

Dockerfunctiontest.bash

Less confusion in the future.

Share:
8,730
Bobby
Author by

Bobby

Updated on September 18, 2022

Comments

  • Bobby
    Bobby over 1 year

    I have checked the previous post on this error. still did not get the solution working. Here is my bash script. can some one help me figure out the issue . I have used https://www.shellcheck.net/ to see any error. I did not find any.

    Error:

    Dockerfunctiontest.sh: 2: Dockerfunctiontest.sh: Syntax error: "(" unexpected
    

    script:

    #!/bin/bash
    function BuildSimpleContainer ()
    {
    docker search mariadb
    docker pull mariadb:latest
    docker run --name mariadbtestfour -e MYSQL_ROOT_PASSWORD=mypass -d mariadb --log-bin --binlog-format=MIXED
    docker exec -it mariadbtest bash
    apt-get -qq update
    apt-get -qq -y install curl
    apt-get -qq -y install wget
    apt-get update
    apt-get install apt-transport-https
    apt-get update
    cd /home
    mkdir mdsd
    cd mdsd/
    wget '<blob url to pfx file>'
    echo "certififcate Downloaded"
    wget '<blob url file1>'
    echo "file1 Downloaded"
    wget 'blob url file2'
    echo "file2 Downloaded"
    }   
    BuildSimpleContainer
    
    • Admin
      Admin about 6 years
      How do you call this script?
    • Pankaj Goyal
      Pankaj Goyal about 6 years
      @tomasz has hit it right on the nose. The script is being executed by sh, not bash, and is choking on the function declaration syntax.
    • Stéphane Chazelas
      Stéphane Chazelas about 6 years
  • roaima
    roaima about 2 years
    Why are you recommending the least portable version? The POSIX variant without the word function would seem to be the canonical variant
  • ilkkachu
    ilkkachu about 2 years
    How do you know they're running that with a Korn-compatible shell then? Also, the error message is exactly what Dash would give, and it only supports the POSIX-style function definition. function foo would be valid syntax in Dash, but it'd try to run a command called function. (Dash is /bin/sh in Debian and Ubuntu.)