Are there naming conventions for variables in shell scripts?

118,183

Solution 1

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.

Solution 2

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

Solution 3

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_]).
Share:
118,183

Related videos on Youtube

Garrett Hall
Author by

Garrett Hall

Updated on September 18, 2022

Comments

  • Garrett Hall
    Garrett Hall almost 2 years

    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?

    • Admin
      Admin almost 12 years
      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.
    • Admin
      Admin almost 12 years
      'environment variables' is indeed the proper name, I'll include it in my answer.
    • Admin
      Admin about 8 years
      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.
    • Admin
      Admin about 6 years
  • jw013
    jw013 almost 12 years
    @GarrettHall That's entirely up to you. Once you pick one stick with it. Consistency is more important than the actual choice.
  • jippie
    jippie almost 12 years
    matter of taste? I personally like the C-style camelCase because it is shorter and doesn't use the ugly underscore. Taste, style, ...
  • janos
    janos almost 12 years
    matter of taste? I personally like underscore separated, easier to read.
  • Charles Duffy
    Charles Duffy over 9 years
    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).
  • yrajabi
    yrajabi about 8 years
    @GarrettHall What I do is this plus CamelCase for local variables that I exclusively allow to be referenced inside child functions--when I do allow that. Just a convention, but helps keep scopes clean.
  • smonff
    smonff over 7 years
    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.
  • Nic Szerman
    Nic Szerman almost 3 years
    @jippie code in python and underscore won't be ugly to you anymore