在 bash 中是否有命名“私有函数”的约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2289293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:41:27  来源:igfitidea点击:

Is there a convention for naming 'private functions' in bash?

bashshell

提问by yic

Is there a convention for naming private functions in bash? I have a bash module with some private functions, wondering if I should start their names with underscore. So far I haven't seen any convention.

在 bash 中是否有命名私有函数的约定?我有一个带有一些私有函数的 bash 模块,想知道我是否应该用下划线开头它们的名字。到目前为止,我还没有看到任何会议。

回答by John Kugelman

For what it's worth, Red Hat's /etc/init.d/functionsscript uses double underscores.

就其价值而言,Red Hat 的/etc/init.d/functions脚本使用双下划线。

# __umount_loop awk_program fstab_file first_msg retry_msg umount_args
# awk_program should process fstab_file and return a list of fstab-encoded
# paths; it doesn't have to handle comments in fstab_file.
__umount_loop() {
    # ...
}

# Similar to __umount loop above, specialized for loopback devices
__umount_loopback_loop() {
    # ...
}

# __proc_pids {program} [pidfile]
# Set $pid to pids from /var/run* for {program}.  $pid should be declared
# local in the caller.
# Returns LSB exit code for the 'status' action.
__pids_var_run() {
    # ...
}

# A sed expression to filter out the files that is_ignored_file recognizes
__sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'

回答by DVK

I'm not aware of any formal bash-specific conventions, but starting private identifiers with underscore is a fairly widespread language-independent convention (I encountered it on anything from C to Perl to Java to shell scripts).

我不知道任何正式的特定于 bash 的约定,但以下划线开头的私有标识符是一个相当普遍的独立于语言的约定(我在从 C 到 Perl 到 Java 到 shell 脚本的任何东西上都遇到了它)。