我在 bash_profile 文件中定义了一个函数。
pse(){
ps -ef|grep $1
}
现在我可以从命令行调用这个函数了。喜欢
pse kafka
它工作正常,因为我可以看到正确的输出。 但是当我试图在脚本文件中调用 pse 函数时,它显示“找不到命令”错误。我错过了什么吗?我们是否不允许从 shell 脚本调用 bash_profile 中定义的函数? 操作系统:MacOsX 优胜美地
最佳答案
您可以 export
来自父 shell 的函数以使其在子进程中可用:
export -f pse
来自手册:
export
export [-fn] [-p] [name[=value]]
Mark each name to be passed to child processes in the environment. If the -f option is supplied, the names refer to shell functions; otherwise the names refer to shell variables. The -n option means to no longer mark each name for export. If no names are supplied, or if the -p option is given, a list of names of all exported variables is displayed. The -p option displays output in a form that may be reused as input. If a variable name is followed by =value, the value of the variable is set to value.
The return status is zero unless an invalid option is supplied, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a shell function.
关于bash - 在脚本文件中运行定义的函数时找不到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33921371/