Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a convention for naming a constant in Bash?

In shell scripting, even though I use Java or Python style naming convention, I am still unclear about naming a constant.

Many conventions suggest me to use "capital letter" together with "underscore" for naming a constant e.g. MY_CONSTANT, PI. But in Bash, this may conflict with environment variables.

So, what is the right naming convention for Bash constants?

like image 486
fronthem Avatar asked Aug 21 '15 15:08

fronthem


People also ask

What is the convention for naming a constant?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.

What is the naming conventions for variables and constants?

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed. Variable names should be short yet meaningful.


1 Answers

Together with the question you are linking, there is another related question in Unix & Linux: Are there naming conventions for variables in shell scripts?.

There you can find a couple of good answers:

Variables that are introduced by the operating system or start up scripts etc. are usually all in CAPITALS, these are called 'envrironment variables'.

To prevent your own variables from conflicting with environment variables, it is a good practice to use lower case.

Together with a Shell Style Guide link, where you can find:

Naming Conventions

Function Names

▶ Lower-case, with underscores to separate words. Separate libraries with ::. Parentheses are required after the function name. The keyword function is optional, but must be used consistently throughout a project.

Variable Names

▶ As for function names.

Constants and Environment Variable Names

▶ All caps, separated with underscores, declared at the top of the file.

There is no suggested convention in man bash, just note the "be careful with uppercase" warning.

like image 135
fedorqui 'SO stop harming' Avatar answered Sep 19 '22 04:09

fedorqui 'SO stop harming'