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?
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:
Constants and Environment Variable Names: All caps, separated with underscores, declared at the top of the file. Ex:
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.
- 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 (