[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices

Parameter Substitution in the Korn Shell or POSIX Shell

The Korn Shell, or POSIX shell, enables you to do parameter substitutions.

This section discusses the following:

Parameters in the Korn Shell

A parameter is defined as the following:

The typeset special built-in command assigns values and attributes to named parameters. The attributes supported by the Korn shell are described with the typeset special built-in command. Exported parameters pass values and attributes to the environment.

The value of a named parameter is assigned by:

Name=Value [ Name=Value ] ...

If the -i integer attribute is set for the Name parameter, the Value parameter is subject to arithmetic evaluation. Refer to Arithmetic Evaluation in the Korn Shell or POSIX Shell for more information about arithmetic expression evaluation.

The shell supports a one-dimensional array facility. An element of an array parameter is referenced by a subscript. A subscript is denoted by an arithmetic expression enclosed by brackets ([ ]). To assign values to an array, use set -A Name Value ... . The value of all subscripts must be in the range of 0 through 511. Arrays need not be declared. Any reference to a named parameter with a valid subscript is legal and an array will be created, if necessary. Referencing an array without a subscript is equivalent to referencing the element 0.

Positional parameters are assigned values with the set special command. The $0 parameter is set from argument 0 when the shell is invoked. The $ character is used to introduce parameters that can be substituted.

Parameter Substitution

The following are substitutable parameters:

${Parameter} The shell reads all the characters from the ${ to the matching } as part of the same word, even if that word contains braces or metacharacters. The value, if any, of the specified parameter is substituted. The braces are required when the Parameter parameter is followed by a letter, digit, or underscore that is not to be interpreted as part of its name, or when a named parameter is subscripted.

If the specified parameter contains one or more digits, it is a positional parameter. A positional parameter of more than one digit must be enclosed in braces. If the value of the variable is an * or an @), each positional parameter, starting with $1, is substituted (separated by a field separator character). If an array identifier with a subscript * or an @ is used, then the value for each of the elements (separated by a field separator character) is substituted.

${#Parameter} If the value of the Parameter parameter is an * or an @, the number of positional parameters is substituted. Otherwise, the length specified by the Parameter parameter is substituted.
${#Identifier[*]} The number of elements in the array specified by the Identifier parameter is substituted.
${Parameter:-Word} If the Parameter parameter is set and is not null, then its value is substituted; otherwise, the value of the Word parameter is substituted.
${Parameter:=Word} If the Parameter parameter is not set or is null, then it is set to the value of the Word parameter. Positional parameters cannot be assigned in this way.
${Parameter:?Word} If the Parameter parameter is set and is not null, then substitute its value. Otherwise, print the value of the Word variable and exit from the shell. If the Word variable is omitted, then a standard message is printed.
${Parameter:+Word} If the Parameter parameter is set and is not null, then substitute the value of the Word variable.
${Parameter#Pattern} | ${Parameter##Pattern} If the specified shell Pattern parameter matches the beginning of the value of the Parameter parameter, then the value of this substitution is the value of the Parameter parameter with the matched portion deleted. Otherwise, the value of the Parameter parameter is substituted. In the first form, the smallest matching pattern is deleted. In the second form, the largest matching pattern is deleted.
${Parameter%Pattern} | ${Parameter%%Pattern} If the specified shell Pattern matches the end of the value of the Parameter variable, then the value of this substitution is the value of the Parameter variable with the matched part deleted. Otherwise, substitute the value of the Parameter variable. In the first form, the smallest matching pattern is deleted; in the second form, the largest matching pattern is deleted.

In the previous expressions, the Word variable is not evaluated unless it is to be used as the substituted string. Thus, in the following example, the pwd command is executed only if the -d flag is not set or is null:

echo ${d:-$(pwd)}

Note: If the : is omitted from the previous expressions, the shell checks only whether the Parameter parameter is set.

Predefined Special Parameters

The following parameters are automatically set by the shell:

@ Expands the positional parameters, beginning with $1. Each parameter is separated by a space.

If you place " around $@, the shell considers each positional parameter a separate string. If no positional parameters exist, the shell expands the statement to an unquoted null string.

* Expands the positional parameters, beginning with $1. The shell separates each parameter with the first character of the IFS parameter value.

If you place " around $*, the shell includes the positional parameter values in double quotation marks. Each value is separated by the first character of the IFS parameter.

# Specifies the number (in decimals) of positional parameters passed to the shell, not counting the name of the shell procedure itself. The $# parameter thus yields the number of the highest-numbered positional parameter that is set. One of the primary uses of this parameter is to check for the presence of the required number of arguments.
- Supplies flags to the shell on invocation or with the set command.
? Specifies the exit value of the last command executed. Its value is a decimal string. Most commands return 0 to indicate successful completion. The shell itself returns the current value of the $? parameter as its exit value.
$ Identifies the process number of this shell. Because process numbers are unique among all existing processes, this string of up to 5 digits is often used to generate unique names for temporary files.

The following example illustrates the recommended practice of creating temporary files in a directory used only for that purpose:

temp=$HOME/temp/$$
ls >$temp
.
.
.
rm $temp
! Specifies the process number of the most recent background command invoked.
zero (0) Expands to the name of the shell or shell script.

Variables Set by the Korn Shell or POSIX Shell

The following variables are set by the shell:

underscore (_) Indicates initially the absolute path name of the shell or script being executed as passed in the environment. Subsequently, it is assigned the last argument of the previous command. This parameter is not set for commands that are asynchronous. This parameter is also used to hold the name of the matching MAIL file when checking for mail.
ERRNO Specifies a value that is set by the most recently failed subroutine. This value is system-dependent and is intended for debugging purposes.
LINENO Specifies the line number of the current line within the script or function being executed.
OLDPWD Indicates the previous working directory set by the cd command.
OPTARG Specifies the value of the last option argument processed by the getopts regular built-in command.
OPTIND Specifies index of the last option argument processed by the getopts regular built-in command.
PPID Identifies the process number of the parent of the shell.
PWD Indicates the present working directory set by the cd command.
RANDOM Generates a random integer, uniformly distributed between 0 and 32767. The sequence of random numbers can be initialized by assigning a numeric value to the RANDOM variable.
REPLY Set by the select statement and by the read regular built-in command when no arguments are supplied.
SECONDS Specifies the number of seconds since shell invocation is returned. If this variable is assigned a value, then the value returned upon reference will be the value that was assigned plus the number of seconds since the assignment.

Variables Used by the Korn Shell or POSIX Shell

The following variables are used by the shell:

CDPATH Indicates the search path for the cd (change directory) command.
COLUMNS Defines the width of the edit window for the shell edit modes and for printing select lists.
EDITOR If the value of this parameter ends in emacs, gmacs, or vi, and the VISUAL variable is not set with the set special built-in command, then the corresponding option is turned on.
ENV If this variable is set, then parameter substitution is performed on the value to generate the path name of the script that will be executed when the shell is invoked. This file is typically used for alias and function definitions.
FCEDIT Specifies the default editor name for the fc regular built-in command.
FPATH Specifies the search path for function definitions. This path is searched when a function with the -u flag is referenced and when a command is not found. If an executable file is found, then it is read and executed in the current environment.
HISTFILE If this variable is set when the shell is invoked, then the value is the path name of the file that will be used to store the command history.
HISTSIZE If this variable is set when the shell is invoked, then the number of previously entered commands that are accessible by this shell will be greater than or equal to this number. The default is 128.
HOME Indicates the name of your login directory, which becomes the current directory upon completion of a login. The login program initializes this variable. The cd command uses the value of the $HOME parameter as its default value. Using this variable rather than an explicit path name in a shell procedure allows the procedure to be run from a different directory without alterations.
IFS Specifies IFS (internal field separators), normally space, tab, and newline, used to separate command words that result from command or parameter substitution and for separating words with the regular built-in command read. The first character of the IFS parameter is used to separate arguments for the $* substitution.
LANG Provides a default value for the LC_* variables.
LC_ALL Overrides the value of the LANG and LC_* variables.
LC_COLLATE Determines the behavior of range expression within pattern matching.
LC_CTYPE Defines character classification, case conversion, and other character attributes.
LC_MESSAGES Determines the language in which messages are written.
LINES Determines the column length for printing select lists. Select lists print vertically until about two-thirds of lines specified by the LINES variable are filled.
MAIL Specifies the file path name used by the mail system to detect the arrival of new mail. If this variable is set to the name of a mail file and the MAILPATH variable is not set, then the shell informs the user of new mail in the specified file.
MAILCHECK Specifies how often (in seconds) the shell checks for changes in the modification time of any of the files specified by the MAILPATH or MAIL variables. The default value is 600 seconds. When the time has elapsed, the shell checks before issuing the next prompt.
MAILPATH Specifies a list of file names separated by colons. If this variable is set, then the shell informs the user of any modifications to the specified files that have occurred during the period, in seconds, specified by the MAILCHECK variable. Each file name can be followed by a ? and a message that will be printed. The message will undergo variable substitution with the $_ variable defined as the name of the file that has changed. The default message is you have mail in $_.
NLSPATH Determines the location of message catalogs for the processing of LC_MESSAGES.
PATH Indicates the search path for commands, which is an ordered list of directory path names separated by colons. The shell searches these directories in the specified order when it looks for commands. A null string anywhere in the list represents the current directory.
PS1 Specifies the string to be used as the primary system prompt. The value of this parameter is expanded for parameter substitution to define the primary prompt string, which is a $ by default. The ! character in the primary prompt string is replaced by the command number.
PS2 Specifies the value of the secondary prompt string, which is a > by default.
PS3 Specifies the value of the selection prompt string used within a select loop, which is #? by default.
PS4 The value of this variable is expanded for parameter substitution and precedes each line of an execution trace. If omitted, the execution trace prompt is a +.
SHELL Specifies the path name of the shell, which is kept in the environment.
SHELL PROMPT When used interactively, the shell prompts with the value of the PS1 parameter before reading a command. If at any time a new line is entered and the shell requires further input to complete a command, the shell issues the secondary prompt (the value of the PS2 parameter).
TMOUT Specifies the number of seconds a shell waits inactive before exiting. If the TMOUT variable is set to a value greater than zero (0), the shell exits if a command is not entered within the prescribed number of seconds after issuing the PS1 prompt. (Note that the shell can be compiled with a maximum boundary that cannot be exceeded for this value.)

Note: After the timeout period has expired, there is a 60-second pause before the shell exits.
VISUAL If the value of this variable ends in emacs, gmacs, or vi, then the corresponding option is turned on.

The shell gives default values to the PATH, PS1, PS2, MAILCHECK, TMOUT, and IFS parameters, but the HOME, SHELL, ENV, and MAIL parameters are not set by the shell (although the HOME parameter is set by the login command).

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]