如何找到内置 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
要编辑函数,请打开包含函数定义的文件(您从上面找到的)。编辑函数并保存文件。然后将其源到您的 shell 中,如下所示:
$ . /path/to/function_file
函数通常存储在.bashrc
文件中(或在 中/etc/bash.bashrc
,也存在/etc/bashrc
于某些系统中)。SuperUser 的这个答案有一些关于.bashrc
文件是什么的很好的细节。同样, Unix & Linux 站点上的这个问题详细说明了何时最好使用别名、何时编写脚本以及何时编写函数。