让 .bashrc 中的函数从其他 bash 脚本调用的聪明方法?

Smart way to let function in .bashrc called from other bash script?

我想从 test.sh

调用 .bashrc 中的 fun1

猫test.sh

#!/bin/bash
fun1

fun1在.bashrc中应该写成如下格式:

fun1(){
#do something
}
export -f fun1

我想让 .bashrc 中的所有函数(从 fun2 到 funm)从其他 bash 脚本调用,在最后一行为每个函数添加 export -f funm函数,10 个函数产生 10 行 export -f funm,就像在 .bashrc 文件中一样。

有实现目标的聪明方法吗?

.bashrcfor interactive use。如果您要在脚本中使用它们,请不要在那里定义函数。

如果您想创建一个包含脚本中使用的函数的文件,请将其设为一个单独的文件,并从您的脚本(以及 .bashrc)获取该文件。将功能放在例如~/lib/bash/myfunctions 并放

. ~/lib/bash/myfunctions

在你的脚本中。