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

$ 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

如您所见,脚本./test.sh无法调用函数myfunc,是否有方法使该函数对脚本可见?

最佳答案

尝试

$ export -f myfunc

在父shell中,以export函数。

关于bash - Bash-如何调用在父Shell中声明的函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2199455/

10-13 07:26