12

Trying to run some bash functions , but keep encountering syntax error: "(" unexpected. I've try'ed removing the brackets,parenthesis etc etc...nothing seems to work.

$ bash --version
GNU bash, version 4.3.46(1) release

#!/bin/bash
function hello () {
echo "Hello world"
}
5
  • How are you running the script? – muru Oct 9 '16 at 13:35
  • With full permissions. sh hello.sh – James A Oct 9 '16 at 13:35
  • 1
    see ryanstutorials.net/bash-scripting-tutorial/bash-functaions.php – Rinzwind Oct 9 '16 at 13:36
  • I read his tutorials last night and followed them to see where I was going wrong...but I still get a syntax error which is very confusing. Should't the shebang at least make the function compatible even if Ubuntu is running a different shell? – James A Oct 9 '16 at 13:43
  • Problem solved. I was using $ sh hello.sh ---> I get error . I should of been using ./hello.sh to run it correctly.I'm guessing the shebang gets over written and the default dash shell used? – James A Oct 9 '16 at 14:23
21

If you are running the script with sh hello.sh, the interpreting shell will not be the one mentioned in the shebang line, but /bin/sh will be used. In case of Debian and Ubuntu by default this will be dash.

So to run your script correctly with a bash, use either of the following.

/bin/bash hello.sh

or

chmod +x hello.sh
./hello.sh

Alternatively you also could set bash as the /bin/sh.

dpkg-reconfigure dash 
1
  • Thank you for response.No idea why it was down voted :/ – James A Oct 9 '16 at 14:37
4

First:

The syntax error is because of (). Remove () from the file like this:

#!/bin/bash
function hello {
echo "Hello world"
}

or you can just run the following command to edit the file for you:

sed -i 's/() //g' hello.sh

You should now be able to run the file with the desired result.


Alternatively:

You could add lines 2, 3, and 4 to your ~/.bashrc file.

function hello () {
echo "Hello world"
}

Remember, do not use sudo to edit your ~/.bashrc file!

After you add the lines to the file, run the following command to restart bash or "source" your .bashrc file:

. ~/.bashrc

You should now be able to run the comand hello and "Hello world" should print in the terminal.


Also:

You could edit the file to say this instead:

#!/bin/bash
echo "Hello world"

and name the file hello and save the file to /usr/local/bin.

After doing all of that, make the file executable by running the following command:

sudo chmod +x /usr/local/bin/hello

You should now be able to run the comand hello and "Hello world" should print in the terminal.


Finally:

A third option would be to add the following line to your ~/.bashrc file:

alias hello='echo "Hello world"'

Then, source your .bashrc file using the following command:

. ~/.bashrc 
2
  • Thank you for you responses....I've tried a few and found the answer simple enough. Instead of using $sh hello.sh... ./hello.sh works fine.It must be the shebang been over written ^^ – James A Oct 9 '16 at 14:17
  • Why was Thomas's answer down voted but? Was he not correct? – James A Oct 9 '16 at 14:21
2

GNU Bash is the shell used by default in terminals on Ubuntu. However when scripts are executed on system boot then dash is used, as it is dash that is /bin/sh.

Won't work-->

$ sh hello.sh

Will work -->

$./hello.sh

Problem solved

1

According to your Script:

#!/bin/bash
function hello () {
    echo "Hello world"
}

#call this function as follow:
hello   # Syntax Correct
hello() # Syntax Error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.