System information
An Introduction to Shells in General
Axis Communications AB provides NO support for application development of any kind. The information
here is provided "as is", and there is no guarantee that any of the examples shown will work in your
particular application.
Revision 1.02 October 2002 64
5.5 Using Variables
As is the case with almost any language, the use of variables is very important in shell programs.
You can assign a value to a variable simply by typing the variable name followed by the equal
sign (=) and the value you want to assign to the variable. For example, if you wanted to assign a
value of 5 to the variable count, you would enter:
5.6 Built-in Shell Variables
The shell is aware of special kinds of variable called positional parameters. Positional
parameters are used to refer to the parameters that were passed to the shell program on the
command line or a shell function by the shell script that invoked the function.
When you run a shell program that requires or supports a number of command line options, each
of these options is stored in a positional parameter. The first parameter is stored in a variable
named 1; the second parameter is stored in a variable named 2, and so on. The shell reserves
these variable names 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.
Variable Symbol Description
$?
Contains the exit value returned by the last executed command
$$
Contains the process ID number of the shell
$!
The process number of the most recent asynchronously executed
command.
$-
Contains the flags that were passed to the shell when it was invoked or
flags that were set using the set command.
$#
Contains the number of arguments to the shell.
$*
Contains the current argument list. By itself $* is equivalent to $1, $2
and so on, up to the number of arguments. The construct "$*" is
equivalent to "$1, $2 ...."
count=5
Note 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 non-typed interpretive language. This means that
you can use the same variable to store character strings that you use to store integers.
Once you have stored a value in 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 wanted to print the value
stored in the count variable to the screen, you would enter the following command:
echo $count
If you omitted the $ from the preceding command, the echo command would simply display the
word count.