如何列出所有导出的bash函数?

问题描述 投票:3回答:3

在bash中,我们可以通过以下方式导出函数:

fname(){
  echo "Foo"
}

export -f fname

在这种情况下,功能fname被导出。但是如何列出此功能或其他导出的功能? AFAIK,命令exportexport -p可用于显示所有导出/包含的变量,但这不包括函数。

bash shell
3个回答
6
投票

以下将按名称列出所有导出的功能:

declare -x -F

如果您还想查看功能代码,请使用:

declare -x -f 

有关详细信息,请参见help declare


0
投票

declare是要使用的命令。

这里是设置和导出某些功能并全部列出或仅列出其中一个功能的示例:

$ foo() { echo "Foo"; }
$ export -f foo
$ bar() { echo "Bar"; }
$ export -f bar
$
$ declare -f
bar ()
{
    echo "Bar"
}
declare -fx bar
foo ()
{
    echo "Foo"
}
declare -fx foo
$
$ declare -f foo
foo ()
{
    echo "Foo"
}
$

0
投票

所选解决方案的输出是:

declare -fx exported_function_one declare -fx exported_function_two

就我而言,因为我只想要函数的名称,所以我这样做了:

exported_functions=$(declare -x -F | sed 's/declare -fx//')

哪个输出:

exported_function_one exported_function_two

希望它可以帮助某人:D

© www.soinside.com 2019 - 2024. All rights reserved.