Are there naming conventions for variables in shell scripts?

Environment variables or shell variables that are introduced by the operating system, shell startup scripts, or by the shell itself 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 variable names.


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


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