如何在 Bash 中打印函数定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9894872/
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-18 01:52:32  来源:igfitidea点击:

How to print a function definition in Bash?

bash

提问by Omar Jalalzada

I have defined a few different functions in my .bash_profile. I usually remember the name of function but want to take a quick peek in the code before I run it.

我在我的.bash_profile. 我通常会记住函数的名称,但想在运行之前快速浏览一下代码。

In my .bash_profileI have the following:

在我的.bash_profile我有以下内容:

gpm () {
  echo "git pull origin master"
  git pull origin master
}

Now I want to run something like this in Bash:

现在我想在 Bash 中运行这样的东西:

$ <something> gpm

Result expected: Don't execute the function just print out the function definition itself.

预期结果:不要执行函数,只是打印出函数定义本身。

回答by sorpigal

EDIT: The best answer isn't this one, but the other one below.

编辑:最好的答案不是这个,而是下面的另一个。

What this answer used to say is that you can get a function definition in bash using the typebuiltin, e.g. type gpm. However, using declareas described in the other answer is better in every way.

这个答案曾经说的是,您可以使用type内置函数在 bash 中获得函数定义,例如type gpm. 但是,declare按照其他答案中的描述使用在各方面都更好。

回答by glenn Hymanman

declare -f gpmwill just print the function definition of function gpmwith no other text.

declare -f gpm将只打印函数的函数定义,gpm没有其他文本。