Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

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.

like image 372
yic Avatar asked Feb 18 '10 14:02

yic


People also ask

What is $@ in bash?

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.

What does $_ mean in bash?

$_ (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.

Do bash variables need to be uppercase?

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.

Are bash function variables global?

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.


2 Answers

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'
like image 167
John Kugelman Avatar answered Sep 30 '22 01:09

John Kugelman


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).

like image 40
DVK Avatar answered Sep 30 '22 00:09

DVK