是否存在在bash中命名"私有函数"的约定?

yic*_*yic 12 bash shell

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

Joh*_*ica 14

对于它的价值,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'
Run Code Online (Sandbox Code Playgroud)


DVK*_*DVK 6

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

  • 使用 _my_function () { ... } 是我的首选方法,因为它符合谷歌的 shell 指南,并且更容易区分变量 (2认同)