Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables local to the script?
3 Answers
Environment variables or shell variables introduced by the operating system, shell startup scripts, or the shell itself, etc., are usually all in CAPITALS1.
To prevent your variables from conflicting with these variables, it is a good practice to use lower_case variable names.
1A notable exception that may be worth knowing about is the path array, used by the zsh shell. This is the same as the common PATH variable but represented as an array.
-
67
lower_caseunderscore separated orcamelCase?Garrett Hall– Garrett Hall2012-07-11 20:55:49 +00:00Commented Jul 11, 2012 at 20:55 -
15@GarrettHall That's entirely up to you. Once you pick one stick with it. Consistency is more important than the actual choice.jw013– jw0132012-07-11 20:57:20 +00:00Commented Jul 11, 2012 at 20:57
-
7matter of taste? I personally like the C-style
camelCasebecause it is shorter and doesn't use the ugly underscore. Taste, style, ...jippie– jippie2012-07-11 20:57:41 +00:00Commented Jul 11, 2012 at 20:57 -
48matter of taste? I personally like underscore separated, easier to read.janos– janos2012-07-12 08:22:12 +00:00Commented Jul 12, 2012 at 8:22
-
9For completeness, environment variables aren't the only category of conventionally all-uppercase shell variable names -- this rule also applies to builtins (like
PWD,PS4, orBASH_SOURCE).Charles Duffy– Charles Duffy2015-01-07 23:49:36 +00:00Commented Jan 7, 2015 at 23:49
Yes, there are full code style conventions for bash, including variable names. For example, here's Google's Shell Style Guide.
As a summary for the variable names specifically:
Variable Names: Lower-case, with underscores to separate words. Ex:
my_variable_nameConstants and Environment Variable Names: All caps, separated with underscores, declared at the top of the file. Ex:
MY_CONSTANT
-
21These convention are only Google ones for their own open source projects: though they could be very good rules, it could not apply to all projects.smonff– smonff2017-02-04 14:16:33 +00:00Commented Feb 4, 2017 at 14:16
Underscores to separate words seem to be the best way to go.
I have a few reasons to prefer snake_case over camelCase when I'm free to choose:
- Flexible: You can use upper case and lower case (e.g.
MY_CONSTANTandmy_variable); - Consistent: The digits can be separated to make the number more readable (e.g.
1_000_000_000) and this feature is supported in many programming languages; - Common: Common at the point the regex
\whandles underscores like word characters and numbers ([a-zA-Z0-9_]).
-
7I believe we should steer clear of attempts to rationalize stylistic decisions (or opinions in general) because the rationale is usually tenuous and can swing either way with little effort. (Also, it polarizes people, often inciting religious wars and time wasted on immaterial issues.) Specifically in re: your reasons,
camelCasedoes not preclude #2, and #3 is trivially mitigated by considering capitals as additional word boundaries. Re: #1, there is no reason whyLOUD_SNAKEcan't be used withcamelCase—that is a common combination, the former often being for constants/globals.Ezekiel Victor– Ezekiel Victor2023-03-31 02:01:26 +00:00Commented Mar 31, 2023 at 2:01
PATHorHOMEor anything else the shell might reserve in the future.