Bash script error: "function: not found". Why would this appear?

BashShellUnixUbuntuTerminal

Bash Problem Overview


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

Bash Solutions


Solution 1 - Bash

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 - Bash

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 - Bash

ls -la /bin/sh

check the sym link where it point to bash or dash

Solution 4 - Bash

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

Solution 5 - Bash

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.

https://stackoverflow.com/questions/7917018/what-is-the-function-keyword-used-in-some-bash-scripts

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionE TView Question on Stackoverflow
Solution 1 - BashNed DeilyView Answer on Stackoverflow
Solution 2 - BashPrateek JoshiView Answer on Stackoverflow
Solution 3 - BashJuda BarnesView Answer on Stackoverflow
Solution 4 - BashKayView Answer on Stackoverflow
Solution 5 - BashPiotr WadasView Answer on Stackoverflow