bash - 在脚本文件中运行定义的函数时找不到命令

标签 bash shell

我在 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/

相关文章:

bash - 如何在 Shell 脚本中为 "for"语句使用 2 个变量

bash - bash 双括号条件表达式是否没有运算符,在任何地方指定/记录?

shell - 在一个值中获取地址的远程 ip 以在 shell 脚本中使用

python - 用 python 改进 bash 脚本

linux - AWK 根据 B 列的唯一性计算 A 列的出现次数

c++ - 如何从 C++ 程序运行 bash 脚本

linux - 为什么我需要括号 In bash `set -e` 和否定返回码

c - C语言实现linux下shell的输入输出

linux - 如何制作删除文件的shell脚本

linux - 无法将正确的文件移动到 mvfiles