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
内置的更多信息:
Run Code Online (Sandbox Code Playgroud)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.
关于功能,man bash
说明declare
如果extdebug
设置了该选项,则可以提供更多信息:
Run Code Online (Sandbox Code Playgroud)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).
我使用以下函数和类似 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,您可以看到它是如何在实际脚本中使用的。