bash 如何列出在我的 shell 中定义的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4471364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:58:49  来源:igfitidea点击:

How do I list the functions defined in my shell?

bashshellfunctionunix

提问by Dov

I can type alias to show a list of all the aliases.

我可以输入 alias 来显示所有别名的列表。

But for functions, all I can do is grep my .bash_profile.

但是对于函数,我所能做的就是 grep 我的.bash_profile.

That only gets the ones in that file, not those defined in subsidiary files or dynamically.

这只获取该文件中的那些,而不是那些在附属文件中或动态定义的。

Is there a more convenient way to find out what functions are currently defined?

有没有更方便的方法来找出当前定义的函数?

回答by The Archetypal Paul

declare -F

declare -F

Function names and definitions may be listed with the -foption to the declarebuiltin command (see Bash Builtins). The -Foption to declarewill list the function names only (and optionally the source file and line number).

函数名称和定义可能会与内置命令的-f选项一起 列出declare(请参阅 Bash 内置命令)。该-F选项declare将列出函数名只(以及可选的源文件和行号)。

Bash Reference Manual

Bash 参考手册

回答by Randall Bohn

Assuming bash shell:

假设 bash shell:

typeset -f

will list the functions.

将列出功能。

typeset -F

will list just the function names.

将只列出函数名称。

回答by John Lawrence Aspden

declare -F

will give you the names of all functions

会给你所有函数的名称

type function_name

will give you the source for a particular function

将为您提供特定功能的来源

回答by Lri

declare -Factually prints declare commands and not only function names:

declare -F实际上打印声明命令而不仅仅是函数名称:

$ declare -F
declare -f function1
declare -f function2

You can use compgen -A functionto print only function names:

您可以使用compgen -A function仅打印函数名称:

$ compgen -A function
function1
function2

回答by Robert

typesetis obsolete, please use:

排版已过时,请使用:

declare -f

or

或者

declare -f function_name

or

或者

type function_name

回答by AtomicKobold

set | grep " ()"

In place of grepyou can also use fgrepor hgrep(hgrep is my personal favorite, it's grep but it hi-lites the 'grep-ped' result.

您还可以使用fgrephgrep代替grep(hgrep 是我个人的最爱,它是 grep 但它可以简化“grep-ped”的结果。

hgrepcan be found here: ACME Labs hgrep

hgrep可以在这里找到:ACME 实验室 hgrep

回答by DavidT

I'm no expert at this, still learning, but after finding this question and its answer because I wanted the same, I wrote the following (based on the "The Archetypal Paul's declare" answer) to give me ultimately what I was after: a formatted list of both aliases and functions:

我不是这方面的专家,仍在学习,但在找到这个问题及其答案后,因为我想要相同的答案,我写了以下内容(基于“原型保罗的宣言”答案),最终给我我所追求的:别名和函数的格式化列表:

function functionaliaslist() {
    echo
    echo -e "3[1;4;32m""Functions:""3[0;34m"
    declare -F | awk {'print '}
    echo
    echo -e "3[1;4;32m""Aliases:""3[0;34m"
    alias | awk {'print '} | awk -F= {'print '}
    echo
    echo -e "3[0m"
}

That was before I saw Lri's answer, and so extrapolating from that, I replace the declareand aliaslines with appropriate compgencommands instead, to get:

那是在我看到Lri's answer 之前,因此从中推断,我用适当的命令替换declarealiascompgen,以获得:

function functionaliaslist() {
    echo
    echo -e "3[1;4;32m""Functions:""3[0;34m"
    compgen -A function
    echo
    echo -e "3[1;4;32m""Aliases:""3[0;34m"
    compgen -A alias
    echo
    echo -e "3[0m"
}

Woks a treat for what I wanted. Sharing in case it helps anyone else.

炒菜是我想要的。分享以防对其他人有帮助。

There are a multitude of other "actions" available to compgen -A [action](and other options for compgenof course). I found a good write-up herewhich also includes a link to the man page (because man compgendoesn't work in some cases).

还有许多其他“操作”可用compgen -A [action]compgen当然还有其他选项)。我在这里找到了一篇很好的文章,其中还包含一个指向手册页的链接(因为man compgen在某些情况下不起作用)。