Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
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 at 14:58
up vote 39 down vote accepted

Variables that are introduced by the operating system or start up scripts etc. are usually all in CAPITALS, these are called 'envrironment variables'.

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

share|improve this answer
Pretty much what @jw013 says in his comment. – jippie Jul 11 '12 at 20:55
lower_case underscore separated or camelCase? – Garrett Hall Jul 11 '12 at 20:55
@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
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
matter of taste? I personally like underscore separated, easier to read. – janos Jul 12 '12 at 8:22

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

share|improve this answer
The above link is now dead, but I believe this is what it linked to: google.github.io/styleguide/shell.xml – Sam May 13 at 13:38
@Sam, thank you. Yep, that it. It's about time Google stopped using googlecode.com lol – Anonsage May 13 at 17:08

There are 3 naming conventions that you should pay attention to when creating your variable names:

  1. Avoid using all UPPERCASE like the plague. You will avoid name clashes with the Environment Variables, which are always ALL CAPS.
  2. Never start your variable names with a number.
  3. Avoid using special characters in your variable names, with the exception of the underscore character – of course.
share|improve this answer
Hi! Welcome to U+L, and thanks for contributing. I think your answer would be more convincing if you could quote authoritative sources that indicate how pervasive the conventions you mention are. I wouldn't call "don't start with a digit" a convention, since it is a rule enforced by bash. The same applies to your third rule; bash says an identifier is made of "alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier". – dhag Apr 20 '15 at 14:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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