Module 99 PARAMETERSVARIABLESARRAYS

Previous Table of Contents Next


Module 99
PARAMETERS/ VARIABLES /ARRAYS

DESCRIPTION

Variables are names that the shell uses to store values. The shell maintains four types of variables;

   Positional parameters
   Special parameters
   Named variables
   Array variables

Arrays are discussed in the Arrays section. All variables are referenced by preceding them with a dollar sign ($). Refer to the Variable Substitution and Referencing section for further information on referencing the value stored in a variable.

All variables are expanded by the shell before a command is executed. Normally, the data type of a variable defaults to character but the ksh provides a command, typeset , which allows you to set a variable to a certain data type. For a complete explanation of typeset refer to Module 145.

The Environment

A very important concept to understand about the shell and most UNIX processes is their environment. The shell environment is a set of exported variables, which control how the shell functions and provides information for other programs. These exported variables are referred to as environmental variables. When you set a variable (assign a value to it), it may or may not be exported. If it is NOT exported, it is considered a local variable and is only known by the current shell. If a variable has been exported, it is considered global and is known by the current shell and all new child processes. Refer to Module 47 for further information on export command.


NOTE:    
Parent processes do not know about environmental variables declared in child processes. When a child process dies, all variables are lost. The concept of global descends down through child processes but not up.

Positional Parameters

Positional parameters are numbered variables representing the position of arguments passed to the shell script. The name of the shell script is passed as positional parameter 0, argument 1 is passed as positional parameter 1 and so on. Positional parameters are assigned during invocation of the shell. The following example illustrates the positional parameters.

 cj> myscript pp1 pp2 pp3 pp4 pp5 pp6 pp7 pp8 pp9 

Inside myscript $1 would equal pp1, $2 would equal pp2, and so on up to $9.

To reference positional parameters greater than 9 you must enclose the parameter in braces. For instance, to reference positional parameter 18 you would use ${18} .


NOTE:    
Positional parameters may be reset by using the set command. Refer to Module 118.

Special Parameters

Special parameters are automatically set by the shell. The following list describes each of these parameters. To reference the value of the parameter precede it with a $.

The name of the current shell script
# The number of positional parameters (arguments) passed to the shell script updated to reflect the number of parameters set by executing the set command.
$ The process ID of the shell script being executed
! The process ID of the last process submitted to the background for execution
? The exit status of the last command not executed in the background
- The current options in effect for the shell being executed (hyphen or minus sign)
-- The last positional parameter of the last command ( underscore ). For example, if you type the command cat file1 file2 you can then type a command like, vi $_ to edit file2.
* All positional parameters passed to the shell script being executed. Expands as a single argument, separated by the first character of the IFS variable (see Variables section). $* expands to $1 $2 $3 ... .
@ All the positional parameters of the shell script being executed. Expands to separate arguments. $@ expands to $1 $2 ...
ERRNO Contains the error number of the most recent system call. The meaning of the error number is dependent upon your hardware. Look in /usr/include/sys/errno.h for a list of errors.
LINENO Contains the current line number of a shell script being executed
OLDPWD The previous directory before the last cd command
OPTARG Set to the option argument following an argument when you use the getopts command
OPTIND The index of the next option to be processed by the getopts command
PPID The process ID of the parent process
PWD The present working directory (current directory)
RANDOM A random number. It is reassigned each time it is referenced. The random number sequence can be initialized by assigning an integer value.
REPLY Used by the select keyword command and used when no variable is given
SECONDS Contains the number of seconds since you invoked the current shell

C Shell
Remember each of the following must be preceded by a dollar sign ($) when being referenced.
Same as ksh .
?var Is set to one if the variable is defined; otherwise 0.
{?var}
$ Same as ksh .
< Read one line from the standard input(stdin).
argv Contains the list of command line arguments for the current shell. The elements of the array correspond to the positional parameters $1, $2, ...
cwd The full path of the current working directory.
status The status code returned by the most recent command. If the command terminated abnormally, 0200 is added to the status code. Internal commands that fail return a status of 1.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net