93

I'm trying to run a bash script on my Ubuntu machine and it is giving me an error:

function not found

To test, I created the following script which works fine on my laptop but not on my Desktop. Any ideas as to why? My laptop is a mac if that's relevant.

#!/bin/bash

function sayIt {   
   echo "hello world"
}

sayIt

This returns "hello world" on my laptop, but on my Desktop it returns:

run.sh: 3: function not found hello world run.sh: 5: Syntax error: "}" unexpected

1
  • It would be interesting to know how you called the script. Commented Nov 9, 2020 at 10:40

5 Answers 5

166

Chances are that on your desktop you are not actually running under bash but rather dash or some other POSIX-compliant shell that does not recognize the function keyword. The function keyword is a bashism, a bash extension. POSIX syntax does not use function and mandates the use of parenthesis.

$ more a.sh
#!/bin/sh

function sayIt {   
   echo "hello world"
}

sayIt
$ bash a.sh
hello world
$ dash a.sh
a.sh: 3: function: not found
hello world
a.sh: 5: Syntax error: "}" unexpected

The POSIX-syntax works in both:

$ more b.sh
#!/bin/sh

sayIt () {   
   echo "hello world"
}

sayIt
$ bash b.sh
hello world
$ dash b.sh
hello world
Sign up to request clarification or add additional context in comments.

7 Comments

function comes from the korn shell, which pre-dates bash.
+1 Had this exact problem, but the bash would still perform as expected. At least with your explanation I know the why and how! :)
So, how to run a script under bash instead of dash or POSIX-compliant shell?
A likely cause is that the script doesn't have the execute bit set for the executing user, meaning that it's being read as a plain shell script. Make sure that's correct and that the shebang path actually invokes Bash.
For me, i was running a sh script (e.g. sh train.sh -d) but it gave me the error. This answer helped me to realise the problem (i had to write /bin/bash train.sh -d instead)
|
28

I faced the same problem, I then modified the syntax and it worked for me. Try to remove the keyword function and add brackets () after the function name.

#!/bin/bash

sayIt()
{   
   echo "hello world"
}

sayIt

1 Comment

This is not the ideal solution as both syntaxes are defined but your syntax is used for /bin/sh scripts while function name { ... } is used for e.g. /bin/bash. So the OP is probably using the wrong interpreter (She-Bang line says it should be bash, but that line is ignored if you explicitly start the script using sh ...)
9

ls -la /bin/sh

check the sym link where it point to bash or dash

1 Comment

why would #!/bin/bash not take care of this?
-2

For me, I just edited the bash profile and forgot to restart my terminal session.

Comments

-6

Doesn't it require () after function name, or at the call?

function sayIt() { ...
}

sayIt()

? :)

Hmm, actually, on MY mac, it works just as you pasted..

dtpwmbp:~ pwadas$ cat aa.sh 
#!/bin/bash

function sayIt() {   
   echo "hello world"
}

sayIt

dtpwmbp:~ pwadas$ ./aa.sh 
hello world
dtpwmbp:~ pwadas$ 

Compare bash version, AFAIR some older version required "()"s.

dtpwmbp:~ pwadas$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
Copyright (C) 2007 Free Software Foundation, Inc.
dtpwmbp:~ pwadas$ 

Also compare state of shopt options ( man bash ), on both shells, maybe one of them have some compat syntax turned on or off ? "shopt" command without args will list state of options supported.

What is the 'function' keyword used in some bash scripts?

5 Comments

Sadly, I really don't know why my answer for this question was downvoted :/
You should include the () in the function definition but not when calling the function.
@PiotrWadas, I didn't downvote you, but it's really more of a question than an answer.
yea, your answer is really a question about the answer above. so it should be a comment on that answer and not an answer itself
function foo() { is actually more invalid than function foo {. The function foo { syntax is supported on old versions of ksh, and modern shells that try to maintain compatibility with them, but not required to be supported by shells compliant with the 1992 POSIX sh standard. By contrast, foo() { is valid on all POSIX-compliant shells. function foo() { is a hybrid of the two formats, and less widely supported than either on its own. See wiki.bash-hackers.org/scripting/obsolete

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.