You can set the TERM variable by entering the following command line:
TERM=terminal_name export TERMThis is the simplest way to assign a value to a variable. However, there are several other ways to do this:
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 variableThe 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.
$ 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.
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 668-0007 $ num.please Type in last name Niceguy Mr. Niceguy 668-0007 $Notice that the variable name accepts both Mr. and Niceguy as the value.
You can substitute the output of a command for the value of a variable by using command substitution in the following format:
variable=`command`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-19You 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 $
You can assign a positional parameter to a named parameter by using the following format:
var1=$1The 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 var1=$1 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 $