Teach Yourself UNIX Shell Programming in 14 Days

Chapter 3: Shell Variables

Objectives:

This chapter discusses the use of variables in UNIX shell scripts. Objectives important to this chapter are:

  • Shell variables
  • Naming variables
  • Using variables
  • Substitution operators
  • Local vs. environment variables
  • Special variables
  • Using variables as arguments
  • Math with variables
Concepts:

Like variables in C, shell variable names must start with a letter or an underscore. It is best to start with a letter. Follow the initial letter with letters, numbers, and/or underscores.

Unlike C, when you assign a value to a shell variable, you are not allowed to use spaces before or after the equal sign.

	counter=2 # this is right
	counter = 3 # this is wrong

To access the value stored in a variable, use a dollar sign in front of the variable name.

echo $counter 
# this will print the value of counter to the screen.

Like the rest of UNIX, variable names are case sensitive. Spell them one way, including capitalization, and continue to spell them the same way. Use meaningful names wherever possible.

When the shell finds a variable reference in a script command, variable substitution takes place before the command is executed.

A shell variable does not have to be declared before it is used. If you use it before it has a value assigned, however, it will have no value. The command

	echo $brand_new_variable

would echo nothing, if that variable truly had no value assigned to it.

Three ways to reset a variable to no value (null) are:

  • variable=
  • variable=#
  • variable=""

The eval command tells the shell to evaluate what a command means, including variable substitutions, before executing it. The command

	ls | wc -l

means to list the current directory, pipe the list through the wc command and output only the number of lines in the directory listing. Our author suggests assigning the pipe character to a variable like this:

	pipe="|"

and then duplicating the command as

	Ls $pipe wc -l

Of course, this fails. The shell thinks we want a listing of three files. To cause the shell to use the command as intended, we have to tell it to evaluate the command line as a whole, then execute it:

	eval Ls $pipe wc -l

results in the same output as the original line above.

The Bourne shell stores the value of all variables as strings. This means that Bourne does not have numeric variable types. We will use the expr command to force the shell to consider the integer value of strings. (Note: this only works with integer values.) It is used in conjunction with the accent grave character (the French backward accent, like a back-apostrophe, remember?). A code sequence could be:

count=1 
# sets variable count equal to the string "1"

echo $count 
# prints the value of count to the screen

count=$count+1 
# sets the value of count to the string "$count+1"

This sequence does not do math as desired. To get the shell to do integer math change the last line to:

count=`expr $count + 1` 
# sets count equal to 2

Note the use of the accents, the expr command, and the three spaces inside the accents.

You can do addition, subtraction, division and multiplication with the standard operator symbols, but the asterisk must be preceded by a backslash. The backslash means "do not interpret as a wild card". We will use the backslash whenever we want to tell the shell not to use the "special" meaning of the next character.

The shell contains some environment variables that are useful to you. It is a good idea not to redefine them without reason.

  • HOME - the directory you are assigned to log into
  • SHELL - the default shell that can be invoked, for instance from inside vi (can be SH instead)
  • TERM - a definition of the type of terminal you are using
  • MAIL - the directory containing the user's system mailbox
  • USER - the login name used by the current user
  • LOGNAME - same as USER

The set command will let you see the current values for several variables at once.

UNIX commands are divided into two sets: built in commands and disk based commands. You may be familiar with the DOS version of the concept: internal and external commands. The only difference to the user is that disk based commands are separate files and can be lost, erased, or never installed to begin with.