Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 740
E T Avatar asked Sep 18 '12 00:09

E T


People also ask

How do I fix bash command not found?

Install a package Sometimes when you try to use a command and Bash displays the "Command not found" error, it might be because the program is not installed on your system. Correct this by installing a software package containing the command.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does $() mean in bash?

Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

Why can't I run a bash script?

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.

Why is Bash command not found in Linux?

The error gives some hint already when it says “bash: command not found”. Your shell (or Linux system) cannot find the command you entered. There could be three possible reasons why it cannot find the command: The command is basically an executable script and its location is not known

Does shell script throw not found error when run from sh file?

But if entered manually the commands work - Unix & Linux Stack Exchange Shell script throws a not found error when run from a sh file. But if entered manually the commands work

Why can't I define functions in a bash script?

If you define functions in your .bashrc, they are only available in interactive shells, not in scripts. The . command is (almost) equivalent to source, and has nothing to do with . meaning the current directory.


2 Answers

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 
like image 187
Ned Deily Avatar answered Oct 15 '22 19:10

Ned Deily


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 
like image 40
Prateek Joshi Avatar answered Oct 15 '22 18:10

Prateek Joshi