如何找到内置bash函数的源代码?
我知道这是一个功能:
$type -t MY_APP
function
我看到它的代码:
type MY_APP
code
问题是:
- 它存放在哪里
- 如何修改
你可以这样做:
# 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
函数通常存储在.bashrc
文件中(或/etc/bash.bashrc
中,在某些系统中也仅作为/etc/bashrc
存在)。这个来自SuperUser的答案有一些关于.bashrc
文件是什么的好细节;Linux站点详细说明了何时最好使用别名、何时编写脚本以及何时编写函数。