-->
Previous Table of Contents Next


Assigning a Value to a Variable

In all three of the shells discussed earlier, you can assign a value to a variable simply by typing the variable name followed by an equal sign and the value you want to assign to the variable. For example, if you want to assign a value of 5 to the variable count, enter the following command in bash or pdksh:


count=5

With tcsh you must enter the following command to achieve the same results:


set count = 5


Note:  
With the bash and pdksh syntax for setting a variable, make sure that there are no spaces on either side of the equal sign. With tcsh, it doesn’t matter if there are spaces or not.

Notice that you do not have to declare the variable as you would if you were programming in C or Pascal. This is because the shell language is a nontyped interpretive language. This means that you can use the same variable to store character strings that you use to store integers. You store a character string into a variable in the same way that you store the integer into a variable. For example, to set the variable called “name” to have the value “Garry” use these commands:

name=Garry (for pdksh and bash)
set name = Garry (for tcsh)

Accessing the Value of a Variable

After you have stored a value into a variable, how do you get the value back out? You do this in the shell by preceding the variable name with a dollar sign ($). If you want to print the value stored in the count variable to the screen, you do so by entering the following command:


echo $count


Tip:  
If you omit the $ from the preceding command, the echo command displays the word count onscreen.

Positional Parameters and Other Built-In Shell Variables

The shell has knowledge of a special kind of variable called a positional parameter. Positional parameters are used to refer to the parameters that are passed to a shell program on the command line or a shell function by the shell script that invokes the function. When you run a shell program that requires or supports a number of command-line options, each of these options is stored into a positional parameter. The first parameter is stored into a variable named 1, the second parameter is stored into a variable named 2, and so forth. These variable names are reserved by the shell so that you can’t use them as variables you define. To access the values stored in these variables, you must precede the variable name with a dollar sign ($) just as you do with variables you define.

The following shell program expects to be invoked with two parameters. The program takes the two parameters and prints the second parameter that was typed on the command line first and the first parameter that was typed on the command line second.


#program reverse, prints the command line parameters out in reverse #order

echo “$2” “$1”

If you invoke this program by entering


reverse hello there

the program returns the following output:


there hello

Several other built-in shell variables are important to know about when you are doing a lot of shell programming. Table 14.1 lists these variables and gives a brief description of what each is used for.

Table 14.1. Built-in shell variables.

Variable Use

$# Stores the number of command-line arguments that are passed to the shell program.
$? Stores the exit value of the last command that was executed.
$0 Stores the first word of the entered command (the name of the shell program).
$* Stores all the arguments that are entered on the command line ($1 $2 …).
“$@” Stores all the arguments that were entered on the command line, individually quoted (“$1” “$2” …).


Previous Table of Contents Next