Bash - 如何调用父shell中声明的函数?

Ror*_*ory 13 unix bash shell

我正在编写一个调用父shell中声明的函数的bash脚本,但它不起作用.

例如:

$ 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
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,脚本./test.sh无法调用该函数myfunc,是否有某种方法可以使该函数对脚本可见?

unw*_*ind 26

尝试

$ export -f myfunc
Run Code Online (Sandbox Code Playgroud)

在父shell中,导出该函数.

  • 这些事情应该更好地记录下来 (2认同)