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?

  • 1
    The only one that I know of which everyone should follow is all uppercase names should be reserved for the shell. Don't use them to avoid accidentally clobbering something important like PATH or HOME or anything else the shell might reserve in the future. – jw013 Jul 11 '12 at 20:53
  • 3
    Actually, all uppercase names are typically used for environment variables. Some variables (like PATH) are interpreted by the shell, while others (like LANGUAGE or PRINTER) may be interpreted by other programs, but there is nothing otherwise special about them. – jlp Jul 11 '12 at 23:01
  • 'environment variables' is indeed the proper name, I'll include it in my answer. – jippie Jul 12 '12 at 6:45
  • While not authoritative, this Google guide has good suggestions: google.github.io/styleguide/shell.xml. It suggests sticking to all caps only for constants and exported variables, snake case for everything else. Personally I like camel case for my globals since no one else recommends it, which lowers the probability of naming collisions. Plus I like the way they read. – Binary Phile May 22 '16 at 14:58
  • 1
    Similar question on SO: Correct Bash and shell script variable capitalization. – codeforester May 11 at 17:05
up vote 87 down vote accepted

Environment variables or shell variables that are introduced by the operating system or shell startup scripts etc. are usually all in CAPITALS.

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

  • 22
    lower_case underscore separated or camelCase? – Garrett Hall Jul 11 '12 at 20:55
  • 3
    @GarrettHall That's entirely up to you. Once you pick one stick with it. Consistency is more important than the actual choice. – jw013 Jul 11 '12 at 20:57
  • 1
    matter of taste? I personally like the C-style camelCase because it is shorter and doesn't use the ugly underscore. Taste, style, ... – jippie Jul 11 '12 at 20:57
  • 12
    matter of taste? I personally like underscore separated, easier to read. – janos Jul 12 '12 at 8:22
  • 3
    For completeness, environment variables aren't the only category of conventionally all-uppercase shell variable names -- this rule also applies to builtins (like PWD, PS4, or BASH_SOURCE). – Charles Duffy Jan 7 '15 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_name

Constants and Environment Variable Names: All caps, separated with underscores, declared at the top of the file. Ex: MY_CONSTANT

  • The above link is now dead, but I believe this is what it linked to: google.github.io/styleguide/shell.xml – Sam May 13 '16 at 13:38
  • 1
    @Sam, thank you. Yep, that it. It's about time Google stopped using googlecode.com lol – Anonsage May 13 '16 at 17:08
  • 1
    Do you always do what Google says? ;-) – tim.rohrer Aug 20 '16 at 18:32
  • 1
    These 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 Feb 4 '17 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:

  1. Flexible: You can use upper case and lower case (e.g. MY_CONSTANT and my_variable);
  2. 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;
  3. Common: Common at the point the regex \w handles underscores like word characters and numbers ([a-zA-Z0-9_]).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.