6

如何找到内置 bash 函数的来源?

我知道这是一个函数:

$type -t MY_APP
function

我看到它的代码:

type MY_APP
code

问题是:

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

2 回答 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

要编辑函数,请打开包含函数定义的文件(您从上面找到的)。编辑函数并保存文件。然后将其源到您的 shell 中,如下所示:

$ . /path/to/function_file
于 2013-02-15T14:48:11.503 回答
0

函数通常存储在.bashrc文件中(或在 中/etc/bash.bashrc,也存在/etc/bashrc于某些系统中)。SuperUser 的这个答案有一些关于.bashrc文件是什么的很好的细节。同样, Unix & Linux 站点上的这个问题详细说明了何时最好使用别名、何时编写脚本以及何时编写函数。

于 2013-02-15T14:48:24.310 回答