HP-UX Reference (11i v2 03/08) - 1 User Commands N-Z (vol 2)

s
sh-posix(1) sh-posix(1)
The shell supports a limited one-dimensional array facility. An element of an array parameter is refer-
enced by a subscript. A subscript is denoted by a
[, followed by an arithmetic expression, followed by a
]. See the Arithmetic Evaluation subsection. To assign values to an array, use
set -A name value ....
The value of all subscripts must be in the range of
0 through 1023. Arrays need not be declared. Any
reference to a named parameter with a valid subscript is legal and an array is created if necessary.
Referencing an array parameter without a subscript is equivalent to referencing the first element.
If the
-i integer attribute is set for name, the value is subject to arithmetic evaluation.
Positional parameters, parameters denoted by a number, can be assigned values with the
set special
command. Parameter
0 is set from argument zero when the shell is invoked.
Use the prefix character
$ to specify the value of a parameter for substitution.
$parameter
${parameter }
${parameter [subscript ]}
Substitute the value of the parameter, if any. Braces are required when parameter is fol-
lowed by a letter, digit, or underscore that should not be interpreted as part of its name
or when a named parameter is subscripted. If parameter is one or more digits, it is a
positional parameter. A positional parameter of more than one digit must be enclosed in
braces. The shell reads all the characters from
${ to the matching } as part of the same
word, even if it contains braces or metacharacters.
If parameter is
* or @, all the positional parameters, starting with 1, are substituted
(separated by a field separator character). See the Quoting subsection.
If an array parameter with subscript
* or @ is used, the value for each element is substi-
tuted (separated by a field separator character).
${#parameter }
If parameter is * or @, the number of positional parameters is substituted. Otherwise,
the length of the value of the parameter is substituted.
${#parameter [*]}
Substitute the number of elements in the array.
${parameter :-word}
If parameter is set and is nonnull, substitute its value; otherwise, substitute word.
${parameter :=word}
If parameter is not set or is null, set it to word; then substitute the value of the parame-
ter. Positional parameters may not be assigned in this way.
${parameter :?word}
If parameter is set and is nonnull, substitute its value; otherwise, print word and exit
from the shell. If word is omitted, a standard message is printed.
${parameter :+word}
If parameter is set and is nonnull, substitute word; otherwise, substitute nothing.
${parameter #pattern }
${parameter ##pattern }
If the shell pattern matches the beginning of the value of parameter , the value of this
substitution is the value of the parameter with the matched portion deleted; otherwise,
the value of this parameter is substituted. In the former case, the smallest matching pat-
tern is deleted; in the latter case, the largest matching pattern is deleted. These charac-
ters # or % should be escaped by a backslash (\) or quotes (’’).
${parameter %pattern }
${parameter %%pattern }
If the shell pattern matches the end of the value of parameter , the value of parameter
with the matched part is deleted; otherwise, substitute the value of parameter . In the
former, the smallest matching pattern is deleted; in the latter, the largest matching pat-
tern is deleted. These characters # or % should be escaped by a backslash (\) or quotes
(’’).
In the above, word is not evaluated unless it is used as the substituted string. Thus, in the following
example,
pwd is executed only if d is not set or is null:
HP-UX 11i Version 2: August 2003 13 Hewlett-Packard Company Section 1811