Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - How to call a function declared in a parent shell?

Tags:

bash

shell

unix

I am writing a bash script that calls functions declared in the parent shell, but it doesn't work.

For example:

$ function myfunc() { echo "Here in myfunc" ; }
$ myfunc
Here in myfunc
$ cat test.sh 
#! /bin/bash

echo "Here in the script"
myfunc
$ ./test.sh 
Here in the script
./test.sh: line 4: myfunc: command not found
$ myfunc
Here in myfunc

As you can see the script ./test.sh is unable to call the function myfunc, is there some way to make that function visible to the script?

like image 752
Amandasaurus Avatar asked Feb 04 '10 12:02

Amandasaurus


1 Answers

Try

$ export -f myfunc

in the parent shell, to export the function.

like image 183
unwind Avatar answered Oct 04 '22 01:10

unwind