Bash,调用源自脚本的函数?

4 bash function

说我有2个脚本

test1.sh

#!/bin/sh
. ./test2.sh
foo
Run Code Online (Sandbox Code Playgroud)

test2.sh

#!/bin/sh
foo(){
  echo "bar"
}
Run Code Online (Sandbox Code Playgroud)

如果我打电话给第一个脚本就可以

$ ./test1.sh
bar
Run Code Online (Sandbox Code Playgroud)

但是如果我foo在那之后尝试打电话它就行不通.

$ foo
bash: foo: command not found
Run Code Online (Sandbox Code Playgroud)

mou*_*iel 10

执行时./test1.sh,会生成子进程.当test1.sh源时test2.sh,仅在foo()定义时修改该子流程的上下文.一旦test1.sh完成,子进程就会终止,并且您的交互式shell不知道foo().