如何记录我的自定义 bash 函数和别名?

mhu*_*lse 11 bash documentation alias function

问题:

我有多个 bash 函数和别名。我无法记住所有这些,所以我通常最终打开我的.bash_functions.bash_aliases文件来找到我需要的东西。

问题):

如何从 bash 提示符列出可用的函数/别名?

我是否可以使用注释(有点像 PHPDoc)来记录我的 bash 函数/别名?

我只是想要一种简单/不错的方式来输出可用的内容而无需打开文件。运行一个命令并让它吐出我的函数/别名的动态列表会很酷(使用示例将是一个加号)。:)

Joh*_*024 18

要列出活动别名,请运行:

alias
Run Code Online (Sandbox Code Playgroud)

要查看所有活动函数的名称,请运行:

declare -F
Run Code Online (Sandbox Code Playgroud)

要查看所有活动函数的名称和定义,请运行:

declare -f
Run Code Online (Sandbox Code Playgroud)

更多的

别名的信息也是一种脚本友好的格式,包括:

declare -p BASH_ALIASES
Run Code Online (Sandbox Code Playgroud)

man bash提供有关alias内置的更多信息:

   alias [-p] [name[=value] ...]
          Alias with  no  arguments  or  with  the  -p
          option  prints  the  list  of aliases in the
          form alias name=value  on  standard  output.
          When  arguments  are  supplied,  an alias is
          defined for each name whose value is  given.
          A  trailing  space in  value causes the next
          word to be checked  for  alias  substitution
          when  the  alias is expanded.  For each name
          in the argument list for which no  value  is
          supplied, the name and value of the alias is
          printed.  Alias returns true unless  a  name
          is   given  for  which  no  alias  has  been
          defined.
Run Code Online (Sandbox Code Playgroud)

关于功能,man bash说明declare如果extdebug设置了该选项,则可以提供更多信息:

   Function  names  and definitions may be listed with
   the -f option to the  declare  or  typeset  builtin
   commands.  The -F option to declare or typeset will
   list the function names only  (and  optionally  the
   source  file and line number, if the extdebug shell
   option is enabled).
Run Code Online (Sandbox Code Playgroud)

链接

  1. http://ss64.com/bash/alias.html
  2. http://linfo.org/alias.html

  • @ashumeow http://ss64.com/bash/alias.html 上的 ss64.com 文本及其版权和分发条款 http://ss64.com/docs/copyright.html(非商业性!)**BREAK GFDL** 条款——bash 手册的许可,因为它们包含来自 http://www.gnu.org/software/bash/manual/html_node/Aliases.html 的文本:Bash 衍生作品的商业使用手动应该是允许的。由于这个和类似的归因原因,他们没有参考的编译似乎不太好。 (2认同)

Kas*_*erg 8

我使用以下函数和类似 javadoc 的注释为我的脚本创建一个 --help 选项:

PROG=$0 #The program name, used within doHelp

# Print a help message
# doHelp uses lines starting with ## to create the output
# the tags {@param ...} and {@code ...} colorize words
doHelp() {
grep '^##' "${PROG}" |
sed -e 's/^##[[:space:]]*//' |
while read line; do
    if ( echo "${line}" | grep -q '{@param [^}]*}' ); then
        # color parameter and echo evaulated value
        eval echo -e $(echo ${line} | sed \
            -e 's/^\(.*\){@param \([^}]*\)}\(.*\)$/\
            \"\1\\\\E[32;40m\2\\\\E[37;40m\\t(value: \"$\2\")\3\"/');
    else
        # other color commands
        echo -e $(echo ${line} | sed \
            -e 's/{@code \([^}]*\)}/\\E[36;40m\1\\E[37;40m/g');
    fi
done;
}
Run Code Online (Sandbox Code Playgroud)

https://github.com/kaspervandenberg/aida/blob/master/Search/zylabPatisClient/src/main/scripts/generateReport.sh,您可以看到它是如何在实际脚本中使用的。