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 $.

0 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.
0 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.

Named Variables (Keyword Parameters)

The attributes of named variables are listed as follows.

*  Shell variables that can have their values assigned and changed.
*  Their names must begin with an upper or lowercase letter or an underscore.
*  The variable name must contain only upper or lowercase letters, underscores, or digits (0-9).

The general formats for assigning or changing a value of a named variable follow.

     named_variable=value
     keyword_parameter=value command options arguments

The first format assigns a value to the symbolic variable name. In the second format the named parameter (keyword parameter) is assigned a value on the command line that can be referenced by variables inside the shell script. This provides for another way to pass information into the shell script. The parameter is only set for the duration of the command.

Values

The value of a variable can be null or nonnull. The null character equates to nothing, no data assigned. Therefore, nonnull is any ASCII text or numeric value. Nonnull values reflect the fact that the variable is set. Null values do not reflect the fact that the variable is set, since unset variables have a value of null. An unset variable does not exist and thus reference to the variable may return an error or a null value, depending on the circumstances.


NOTE:  
No spaces are allowed on either side of the equal sign (=) when assigning values to variables.



Reserved Named Variables

The following variables are used by the ksh. It is advisable to use these variables only as defined below.

CDPATH A colon-separated list of directories used by the cd command. If the directory argument specified on the cd command line does not start with a /, then the CDPATH is searched for the argument and if a match is found, cd attempts to change to the directory.
COLUMNS The width of your terminal screen in character columns. The default width is 80. Used by the select list and the inline edit window of the shell.
EDITOR If the VISUAL variable is not set and the basename of this variable is set to emacs, gmacs, or vi, then the corresponding in-line editing mode of the shell is used. Refer to the Inline Editing Mode section of Module 50.
ENV The pathname of a file that will be executed when a new shell is invoked. It is commonly used to store aliases and functions. A common filename is .kshrc.
FCEDIT The default editor used by the fc command. If not set, /bin/ed is used.
FPATH A colon-separated list of directories used by the autoload command. Defines a set of directories to be searched for a specified function.
HISTFILE If set when the shell is invoked, then the pathname assigned is used to store the command history. If not set or is not write enabled, $HOME/.sh-history is used.
HISTSIZE If set when the shell is invoked, it specifies how many previous commands may be accessed.
HOME The default directory cd uses if no argument is supplied.
IFS Internal Field Separator. The shell uses the characters in the IFS variable to split the following into fields:
* The result of command substitution.
* The result of positional parameter substitution.
* The input fields read by the read command.
LINES Used by the select command to limit the number of lines to be used in the select list.
LOGNAME The user name that owns the current shell.
MAIL Identifies the file that contains your electronic mail. The shell checks this file for new mail and informs you based on the MAILCHECK variable.
MAILCHECK Specifies how often, in seconds, the shell will check for new mail in the file referenced by $MAIL. The default is 600 seconds. If 0 is used, then the shell checks for new mail after each command is executed.
MAILPATH Identifies a list of files to be checked for the arrival of new mail. Each file is delimited by a colon. If a % sign and text follows a filename, then the text is displayed in place of the default message "you have mail."
PATH Identifies a list of directories to search when the shell attempts to locate a command. An empty field (::) or a field containing a period specifies the current directory.
PS1 The primary shell prompt. Default is $. The value of PS1 is displayed each time the shell is waiting for input from the keyboard.
PS2 The secondary shell prompt. Default is >. Displayed if the shell requires more information to complete the command syntax.
PS3 Used by the select command to request a response. Default is #?.
PS4 Displayed for execution of each command during a shell trace. The default is +. A useful setting is:

PS4='[$LINENO]+'

Identifies the file the shell will use to store accounting information about each command it executes.

SHELL Specifies the shell to be used by commands when you escape to a subshell. The vi and ed command use SHELL when escaping to a subshell. It is also used during startup by the login shell to decide if it should be a restricted shell.
TERM Identifies your terminal type. Must be set for programs that provide screen control (such as vi) to function properly.
TERMINFO Identifies the directory to search for terminal information describing your terminal type.
TERMCAP Identifies the database to scan for terminal information describing your terminal type. TERMINFO replaced termcap in System V Release 2.0. But termcap is still used by other applications.
TMOUT Specifies the number of seconds of inactivity that will cause the shell to automatically log out of the system. If set to 0 or not set (NULL), then time-out is disabled. The message "shell time out in 60 seconds" appears on the terminal. If a key is pressed within 60 seconds the user is not logged off.
TZ Defines the time zone displayed by the date command.
VISUAL Overrides the assigned value of the EDITOR variable. Used for the same purpose.


Previous Table of Contents Next