Programming with the UNIX system shell

Assigning a value to a variable

You can set the TERM variable by entering the following command line:

   export TERM
This is the simplest way to assign a value to a variable. However, there are several other ways to do this: The following sections discuss each of these methods in detail.

Using the read command

The read command used within a shell program allows you to prompt the user of the program for the values of variables. The general format for the read command is:

   read variable
The values assigned by read to variable will be substituted for ``$''variable wherever it is used in the program. If a program executes the echo command just before the read command, the program can display directions such as Type in . . . . The read command will wait until you type a character string, followed by <Return>, and then make that string the value of the variable.

The following example shows how to write a simple shell program called num.please to keep track of your telephone numbers. This program uses the following commands for the purposes specified.

Prompt you for a person's last name.

Assign the input value to the variable name.

Search the file list for this variable.
Your finished program should look like the one displayed here:
   $ cat num.please
   echo Type in the last name:
   read name
   grep $name home/list

Create a file called list that contains several last names and telephone numbers. Then try running num.please.

The next example is a program called mknum, which creates a list. mknum includes the following commands for the purposes shown.

If you want the output of the echo command to be added to the end of list, you must use >> to redirect it. If you use >, list will contain only the last telephone number you added.

Running the cat command on mknum displays the contents of the program. When your program looks like this, you will be ready to make it executable (with the chmod command):

   $ cat mknum
   echo Type in name
   read name
   echo Type in number
   read num
   echo $name $num >> list
   $ chmod u+x mknum

Try out the new programs for your telephone list. In the next example, mknum creates a new listing for Mr. Niceguy. Then num.please gives you Mr. Niceguy's telephone number:

   $ mknum
   Type in name
   Mr. Niceguy
   Type in number
   $ num.please
   Type in last name
   Mr. Niceguy 668-0007
Notice that the variable name accepts both Mr. and Niceguy as the value.

Substituting command output for the value of a variable

You can substitute the output of a command for the value of a variable by using command substitution in the following format:

The output from command becomes the value of variable.

In one of the previous examples on piping, the date command was piped into the cut command to get the correct time. That command line was the following:

   date | cut -c12-19
You can put this in a simple shell program called t that gives you the time.
   $ cat t
   time=`date | cut -c12-16`
   echo The time is: $time
Remember, there are no spaces on either side of the equal sign. Make the file executable, and you will have a program that gives you the time:
   $ chmod u+x t
   $ t
   The time is: 10:36

Assigning values with positional parameters

You can assign a positional parameter to a named parameter by using the following format:

The next example is a simple program called simp.p that assigns a positional parameter to a variable. By running the cat command on simp.p, you can see the contents of this program:
   $ cat simp.p
   echo $var1
Of course, you can also assign to a variable the output of a command that uses positional parameters, as follows:
   person=`who | grep $1`
In the next example, the program log.time keeps track of your whoson program results. The output of whoson is assigned to the variable person, and added to the file login.file with the echo command. The last echo displays the value of $person, which is the same as the output from the whoson command:
   $ cat log.time
   person=`who | grep $1`
   echo $person >> $home/login.file
   echo $person
If you execute log.time specifying maryann as the argument, the system responds as follows:
   $ log.time maryann
   maryann     tty61          Apr 11 10:26

Next topic: Shell programming constructs
Previous topic: Named variables

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004