Using parameter and variable expansion
A $ followed by a number stands for a positional
parameter passed to the script or function. A positional parameter
is represented with either a single digit (except 0) or two or more
digits in braces; for example, 7 and {15} are both valid representations
of positional parameters. For example, if the command:
echo $1
appeared
in a shell script, it would echo the first
positional parameter.Similarly, a $ followed by the name of a shell variable (such as $HOME) stands for the value of the variable.
These constructs are called parameter expansions. In this sense, the term parameter can mean either a positional parameter or a shell variable.
The z/OS® shell also supports more complicated forms of parameter expansions, letting you obtain only part of a parameter value or a modified form of the value.
Parameter expansion | Usage |
---|---|
${parameter:-value} | You can use ${parameter:-value} in
any input to the shell. If parameter currently
has a value and the value is not null (for example, a string without
characters), the foregoing construct stands for the parameter's value.
If the value of the parameter is null, the construct is replaced with
the value shown in the brace brackets. For
example, a shell script might contain:
If
the SHELL variable currently has a value, this simply assigns SHELL its
own current value. However, if the value of SHELL is null,
the given assignment will have the value of /bin/sh. The value
after :– can be thought of as a backup value
in case the parameter itself does not have a value. As another example,
consider:
(This might occur
in a shell script.) If both positional parameters are present and
have a nonnull value, the copy command is just:
However,
if you call the shell script without specifying a second positional
parameter, it uses the backup value of $HOME. The result is
equivalent to:
|
${parameter:=value} | The expansion form ${parameter:=value} is similar to the previous form; the difference is that if the given parameter does not currently have a value, the given value is assigned to parameter, and then the new value of parameter is used. Thus the := form actually assigns a value if the parameter does not already have one. In this case, parameter must be a variable; it cannot be a positional parameter. |
${parameter:?message} | The expansion ${parameter:?message} is
related to the previous two forms. If the value of the given parameter is
null, the given message is displayed. If
the construct is being used inside a shell script, the script ends
with an error status. For example, you might have:
In
this case, the message following the ? is displayed
if there is no second positional parameter. If you omit the message,
the shell prints a standard message. For example, you could just enter:
to get the standard error message. |
${parameter:+replacement} | The construct ${parameter:+replacement} might
be thought of as the opposite of the preceding expansions. If parameter has
not been assigned a value, or has a null value, this construct is
just the null string. If parameter does have
a value, the value is ignored and the replacement value
is used in its place. Thus, if a shell script contains:
the echo command
displays:
if the script
was invoked with a parameter. If no parameter was specified, the echo command
has nothing to echo. |
${parameter#pattern} | The construct ${parameter#pattern} is
evaluated by expanding the value of parameter and
then deleting the smallest leftmost part of the expansion that
matches the given pattern of pathname wildcard
characters. For example, suppose that the variable NAME stands
for a file name. You might use:
to
remove the highest-level directory from the pathname. If:
then:
expands to:
|
${parameter##pattern} | The construct ${parameter##pattern} removes
the largest leftmost part that matches the pattern. For example,
if:
then:
yields:
The wildcard character * stands
for any sequence of characters. In this situation, it stands for everything
up to the final slash. |
${parameter%pattern} | The construct ${parameter%pattern} removes
the smallest rightmost part of the parameter expansion that
matches pattern. Thus if:
then:
stands for:
|
${parameter%%pattern} | Similarly, ${parameter%%pattern} stands
for the expansion of parameter without the longest
rightmost string that matches pattern.
Using the previous example of NAME,
stands
for:
|