In algebra, you learned that you can use letters to stand for unknown quantities in algebraic equations, and that these letters are called variables. In programming, the concept is pretty much the same, although programming variables can hold strings (strings of characters) as well as numbers. You work with two types of variables in shell programming: shell variables and environment variables. The primary difference is that environment variables are available to other scripts or programs that you call from inside your shell program, whereas shell variables are available only to the script itself. Variables in shell programming are loosely typed; they do not have to be declared or typeset before they can be used, whereas in other programming languages they do. All variables in shell programming are stored as strings. Variable AssignmentIn its most basic form, a variable can be set from the command line in the following manner: myVar=5 The value 5 is now stored in the variable myVar. To access the information stored in a variable, precede the variable name with a dollar sign ($). For example echo ${myVar} will print 5 to STDOUT, which as you recall is normally the screen. Note Variable assignment is one place where whitespace does matter. The statement mvar=5 will assign the value 5 to the variable myVar. The statement myVar = 5 will produce an error because the shell will try to interpret myVar as a command name to execute rather than as a variable assignment. The curly braces are optional but can help to improve code readabilitythey make variable names easier to spot when you are quickly scanning code. It's up to you whether you want to use the braces. The value of a variable can also be assigned to another variable. Here's an example: myVarB=$myVar This line assigns whatever is in myVar to the variable myVarB. If myVarB already contains something (for example, the number 5), that value is overwritten by the new assignment. In addition, it is also possible to assign the output of a command to a variable or read input from STDIN ("standard input," which is normally the keyboard) and store it in a variable. You will see how to do this later in the chapter. Creating an environment variable is pretty much the same as creating a shell variable. The only difference is that it must be exported from your script's context before it becomes part of the environment. This is done with the export statement, as shown in this example: MYVAR=5 export MYVAR This creates a shell variable named MYVAR and then exports it so that it is an environment variable that is available to other shells started from within this shell. Variable NamesVariable names are case-sensitive and can contain letters, numbers, and underscores. The variable name cannot begin with a number, though, and it is best to avoid beginning variable names with an underscore. Use descriptive names to make program code easier to read. For example, it is much easier to guess what a variable named avg_rainfall probably contains than to guess what a variable named xyz123 might contain. Caution You cannot use words that are already the names of shell commands as variable names; nor can you use any of the "reserved words" that have special meaning in shell scripting, such as if and case. Tip By convention, local variables use lowercase letters, and environment variables use uppercase letters. However, some people prefer to use a mixture of lowercase and uppercase letters in local variables to make them stand out from other shell commands, which are almost always in all lowercase. |