Bash script error: "function: not found". Why would this appear?
Solution 1
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
Solution 2
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
Solution 3
ls -la /bin/sh
check the sym link where it point to bash or dash
Related videos on Youtube
E T
Updated on July 05, 2022Comments
-
E T almost 2 years
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
-
Timo over 3 yearsIt would be interesting to know how you called the script.
-
-
Piotr Wadas over 11 yearsSadly, I really don't know why my answer for this question was downvoted :/
-
Jacob over 11 yearsYou should include the
()
in the function definition but not when calling the function. -
cdarke over 11 years
function
comes from the korn shell, which pre-dates bash. -
Zuul about 11 years+1 Had this exact problem, but the bash would still perform as expected. At least with your explanation I know the why and how! :)
-
glenn jackman almost 11 years@PiotrWadas, I didn't downvote you, but it's really more of a question than an answer.
-
johnnyB almost 10 yearsyea, your answer is really a question about the answer above. so it should be a comment on that answer and not an answer itself
-
Charles Duffy over 5 years
function foo() {
is actually more invalid thanfunction foo {
. Thefunction 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 -
Alessandro C over 5 yearsSo, how to run a script under bash instead of dash or POSIX-compliant shell?
-
Toby Speight over 5 yearsA 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.
-
Octav over 4 yearsFor 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)
-
voyager42 over 3 yearswhy would #!/bin/bash not take care of this?
-
Timo over 3 years"Chances are that on your desktop you are not actually running under bash". If the first line is /bin/bash, IT IS running under bash I suppose. But: if you start the script with sh .., sh will probably dominate. So you have to start it with ./script.sh in order to "use" the frist shebang line. Thus: It would be interesting to know how op called the script.
-
Ercksen over 2 yearsThis 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 usingsh ...
) -
Thomas Suedbroecker about 2 yearsAwesome! Thanks! for that info!