首页 > 解决方案 > 如何从 PATH 中具有相同名称的脚本调用 bash 函数

问题描述

我在 PATH 中有一个名为 foo 的 bash 脚本

该脚本如下所示:

#!/usr/bin/env bash

foo_type=`type foo`;

if [[ -z "$foo_type" ]]; then
   . "$HOME/.foo/shell.sh"
fi

'foo' "$@"   ### this calls this script not the bash function in shell.sh

正如评论所暗示的问题是'foo'没有调用源bash函数,它最终调用了相同的脚本,所以我进入了一个无限循环,我得到了这个错误:

/.../bin/foo: fork: Resource temporarily unavailable

有谁知道如何调用 source bash 函数?

标签: bashshell

解决方案


如果定义了 bash 函数,这就是已经发生的事情。

您的脚本中的问题是foo_type包含类似foo is /.../bin/foowhich 不为空的内容,因此该脚本永远不会被获取,并且永远不会定义函数。

您可以使用它set -x来调试此问题和其他问题。


推荐阅读