2013-02-15 37 views
6

如何查找内置bash函数的源代码?bash内置函数bash源代码

我知道这是一个函数:

$type -t MY_APP 
function 

我看到它的代码:

type MY_APP 
code 

的问题是:

  1. 它在哪里存储?
  2. 我该如何修改它?

回答

7

你可以这样说:

# Turn on debug 
$ shopt -s extdebug 

# Print out the function's name, line number and file where it was sourced from 
$ declare -F my_function 
my_function 46 /home/dogbane/.bash/.bash_functions 

# Turn off debug 
shopt -u extdebug 

,以编辑功能,打开包含函数定义的文件(即你从上面找到)。编辑功能并保存文件。然后,它源到你的壳,就像这样:

$ . /path/to/function_file 
+0

它的工作原理!我尝试了declare -F my_function,但它仅打印没有调试功能的函数名称。感谢您的好建议。 – idobr 2013-02-15 14:56:35

+0

+1我从来没有使用'extdebug',所以不知道有一种方法可以查看函数源自的文件。非常好。 – chepner 2013-02-15 15:40:04

+0

这太棒了! – 2013-02-15 21:24:01

0

功能通常存储在.bashrc文件(或/etc/bash.bashrc,其中也存在一些系统,只是/etc/bashrc)。来自SuperUser的This answer.bashrc文件有一些很好的细节。同样,this question Unix上的& Linux站点详细介绍何时最好别名,何时脚本以及何时编写函数。

+0

链接的主题很有趣。我在.bashrc和.bashprofile中搜索,定义的函数在其他地方。 @dogbane的回答帮助了我。 – idobr 2013-02-15 14:59:26