使 shell 函数只能在导入文件的范围内找到

Make shell functions only be found in the scope of the importing file

我在一个 shell 文件中声明函数,

# a.sh
foo() { ... }
function bar() { ... }

并通过 source 导入另一个 shell 文件:

# b.sh
source ./a.sh

# invoke foo and bar
foo
bar

现在在shell中,我可以在执行b.sh

后使用foo/bar
$ source b.sh
...

# I can call foo or bar now in the shell (undesirable)
$ foo
...

如何使函数成为导入文件范围内的局部变量,并避免它们污染 global/environmental 个变量?

shell 中没有 "file scope" 这样的东西——只有全局作用域和函数作用域。最接近的是 运行 b.sh 在另一个 shell:

$ b.sh   # run b.sh rather than reading it into the current shell

然后 b.sh 中的所有内容都将在另一个 shell 中,并且在它退出时 "go away" 中。但这适用于 在 b.sh 中定义的所有内容——所有函数、别名、环境和其他变量。

虽然bash不提供直接支持,但您所需要的仍然可以实现:

#!/usr/bin/env bash
# b.sh

if  [[ "${BASH_SOURCE[0]}" = "[=10=]" ]] ;then
    source ./a.sh

    # invoke foo and bar
    foo
    bar
else
    echo "b.sh is being sourced. foo/bar will not be available."
fi

以上并非 100% 可靠,但应该涵盖大多数情况。

可以通过这种方式隔离 私有 shell 函数。

# sourced a.sh

# a_main is exposed public
my_public_a() (
  private_a() {
    echo "I am private_a only visible to my_public_a"
  }

  private_b() {
    echo "I am get_b only visible to my_public_a"
  }

  case "" in
    a) private_a;;
    b) private_b;;
    *) exit;;
  esac
)
# b.sh
source a.sh

my_public_a a
my_public_a b
private_a # command not found
private_b # command not found