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 [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.
By convention, environment variables ( PAGER , EDITOR , ...) and internal shell variables ( SHELL , BASH_VERSION , ...) are capitalized. All other variable names should be lower case. Remember that variable names are case-sensitive; this convention avoids accidentally overriding environmental and internal variables.
By default, every variable in bash is global to every function, script and even the outside shell if you are declaring your variables inside a script.
For what it's worth, Red Hat's /etc/init.d/functions
script uses double underscores.
# __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'
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With