Variables in Korn Shell Programming
- Variables
- Valid Characters
- Scalar
- Array
- Read-Only
- Unsetting
- What's Next
When I was in first grade, my teacher gave us this problem:
_ + 3 = 5
As a budding mathematician, my job was to figure out what number went in the blank space. When I was in ninth grade, my algebra teacher gave us the following problem:
X + 3 = 5
As a budding mathematician, my job was to figure out what number the X represented.
Now that I am older and wiser, I have moved away from such nonsense. Today, I know that it was wrong to use X because it has no meaning. Today, I know that the problem should have been written something like this:
Number_Of_Oranges + 3 = 5
As a budding programmer, I have learned that the use of descriptive variables is better than either a blank space or an X.
In all three examples—blank space, X, and Number_Of_Oranges—what is really happening is that a variable is put in place of a value. We know, because we have been doing this since first grade, that each of these variables has the value of 2.
This chapter talks about variables. It explores the various types of variables allowed in shell programming and shows how to set and unset the variables. Along the way, common errors are pointed out.
This chapter teaches you the following:
How to use variables in a script
How to use the typeset command
How to avoid common errors with variables
How to use arrays in a script
How to customize variable attributes
Case Sensitivity
The basics of variables were discussed in Chapter 1, "The Environment." In review, a variable is simply a way of expressing a value with a name. Hence, variables are called name-value pairs. When designing your own variable, you should keep a number of things in mind. The first is case sensitivity.
Case sensitivity means that the case—uppercase and lowercase—of each letter in the varname is significant. Therefore, two variables can look alike, but if one or more of the letters is of a different case in one of the variables then they are two different variables.
For example, each of these is a different variable:
Apple APple APPle APPLe APPLE aPple aPPle aPPLe aPPLE applE apPle apPLe apPLE appLE ApPle appLe appLE ApPLe ApPLE AppLE
Some standards in programming concerning uppercase and lowercase are probably wise to follow. You might remember from Chapter 1 that environment variables are conventionally (but not necessarily) all uppercased. As an example, here are two environment variables shown in Chapter 1. A shell variable that is exported becomes an environment variable, whether it is uppercase or not:
PS1='$ ' PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/X11R6/bin
Other variables, such as the ones you use to program, should be all—or at least mostly—lowercase. A simple rule when naming a variable is that its name should be as descriptive as its value is important. Therefore, a temporary variable used to hold a value or as a counter could have a name as simple as counter or even x. Another simple rule to remember is that the program you code can be the program you have to change six months from now—so do yourself a favor and name the variables wisely! Variable names should document their purposes.