Parameter substitution in the Korn shell or POSIX shell
The Korn shell, or POSIX shell, lets you perform parameter substitutions.
The following are substitutable parameters:
Item | Description |
---|---|
${ 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 |
${# Parameter} |
If the value of the Parameter parameter
is * or @ , 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:
|
Note: If the
:
is omitted from the previous expressions,
the shell checks only whether the Parameter parameter is
set.