Previous | Table of Contents | Next |
Hour
Variables are words that hold a value. The shell enables you to create, assign, and delete variables. Although the shell manages some variables, it is mostly up to the programmer to manage variables in shell scripts.
This chapter shows you how to
This chapter also explains what environment variables are and how to use them properly. By using variables, you are able to make your scripts flexible and maintainable.
Variables are defined as follows:
name=value
In this example, name is the name of the variable, and value is the value it should hold. For example,
FRUIT=peach
defines the variable FRUIT and assigns it the value peach.
Variables of this type are called scalar variables. A scalar variable can hold only one value at a time. Later in this chapter, you look at a different type of variable called an array variable that can hold multiple values.
Scalar variables are also referred to as name value pairs, because a variables name and its value can be thought of as a pair.
The name of a variable can contain only letters (a to z or A to Z), numbers (0 to 9) or the underscore character (_). In addition, a variables name can start only with a letter or an underscore.
The following examples are valid variable names:
_FRUIT FRUIT_BASKET TRUST_NO_1 TWO_TIMES_2
but
2_TIMES_2_EQUALS_4
is not a valid variable name. To make this a valid name, add an underscore at the beginning of its name:
_2_TIMES_2
Variable names, such as 1, 2 or 11, that start with numbers are reserved for use by the shell. You can use the value stored in these variables, but you cannot set the value yourself.
The reason you cannot use other characters such as !,*, or - is that these characters have a special meaning for the shell. If you try to make a variable name with one of these special characters it confuses the shell. For example, the variable names
FRUIT-BASKET _2*2 TRUST_NO_1!
are invalid names. The error message generated by one of these variable name looks something like the following:
$ FRUIT-BASKET=apple /bin/sh: FRUIT-BASKET=apple: not found.
Variable Values
The shell enables you to store any value you want in a variable. For example,
FRUIT=peach FRUIT=2apples FRUIT=apple+pear+kiwi
The one thing to be careful about is using values that have spaces. For example,
$ FRUIT=apple orange plum
results in the following error message:
sh: orange: not found.
In order to use spaces you need to quote the value. For example, both of the following are valid assignments:
$ FRUIT="apple orange plum" $ FRUIT=`apple orange plum'
The difference between these two quoting schemes is covered in Chapter 9, Quoting.
To access the value stored in a variable, prefix its name with the dollar sign ($). For example, the command
$ echo $FRUIT peach
prints out the value stored in the variable FRUIT, in this case peach.
If you do not use the dollar sign ($) to access the value of a variable, the name of the variable is printed instead of its value. For example,
$ echo FRUIT FRUIT
simply prints out FRUIT, not the value of the variable FRUIT.
The dollar sign ($) is used only to access a variables value, not to define it. For example, the assignment
$ $FRUIT=apple
generates the following warning message
sh: peach=apple: not found
if FRUIT is defined as given previously.
If the variable FRUIT is undefined the error would be
sh: =apple: not found
Remember that when the dollar sign ($) character precedes a variable name, the value of the variable is substituted. For more information on the types of variable substitution available in sh, please consult the section, Variable Substitution, in Chapter 8, Substitution.
The Bourne shell, sh, supports only scalar variables, which are the type of variables you have seen so far. The Korn shell, ksh, extends this to include array variables. Version 2.0 and later of the Bourne Again shell, bash, also support array variables. The examples in the following section assume that you are using either ksh or bash 2.x or later.
Arrays provide a method of grouping a set of variables. Instead of creating a new name for each variable that is required, you can use a single array variable that stores all the other variables.
The difference between an array variable and a scalar variable can be explained as follows. Say that you are trying to represent the chapters in this book as a set of variables. Each of the individual variables is a scalar variable.
Some of these variables might be
CH01 CH02 CH15 CH07
Here is a format for each of the variable names: the letters CH followed by the chapter number. This format serves as a way of grouping these variables together. An array variable formalizes this grouping by using an array name in conjunction with a number that is called an index.
The simplest method of creating an array variable is to assign a value to one of its indices. This is expressed as follows:
name[index]=value
Here name is the name of the array, index is the index of the item in the array that you want to set, and value is the value you want to set for that item.
As an example, the following commands
$ FRUIT[0]=apple $ FRUIT[1]=banana $ FRUIT[2]=orange
set the values of the first three items in the array named FRUIT. You could do the same thing with scalar variables as follows:
$ FRUIT_0=apple $ FRUIT_1=banana $ FRUIT_2=orange
Previous | Table of Contents | Next |