Table of Contents
Parameter Expansion
The character $ is used to introduce parameter expansions. See
PARAMETERS below for a
description of parameters.
- ${name}
-
The value, if any, of the parameter name is substituted. The
braces are required if name is followed by a letter, digit, or
underscore that is not to be interpreted as part of its name. If
name is an array parameter, then the values of each element of
name is substituted, one element per word. Otherwise, the
expansion results in one word only; no word splitting is done on the
result.
- ${+name}
-
If name is the name of a set parameter `1' is substituted,
otherwise `0' is substituted.
- ${name:-word}
-
If name is set and is non-null then substitute its value;
otherwise substitute word.
- ${name:=word}
-
If name is unset or is null then set it to word; the
value of the parameter is then substituted.
- ${name:?word}
-
If name is set and is non-null, then substitute its value;
otherwise, print word and exit from the shell. If word
is omitted, then a standard message is printed.
- ${name:+word}
-
If name is set and is non-null then substitute word; otherwise substitute nothing.
- ${name#pattern}
${name##pattern}
-
If the pattern matches the beginning of the value of
name, then substitute the value of name with the
matched portion deleted; otherwise, just substitute the value of
name. In the first form, the smallest matching pattern is
preferred; in the second form, the largest matching pattern is
preferred.
- ${name%pattern}
${name%%pattern}
-
If the pattern matches the end of the value of name,
then substitute the value of name with the matched portion
deleted; otherwise, just substitute the value of name. In the
first form, the smallest matching pattern is preferred; in the second
form, the largest matching pattern is preferred.
- ${#spec}
-
If spec is one of the above substitutions, substitute the
length in characters of the result instead of the result itself. If
spec is an array expression, substitute the number of elements
of the result.
- ${^spec}
-
Toggle the value of the
RC_EXPAND_PARAM option
for the evaluation of spec.
- ${=spec}
-
Toggle the value of the
SH_WORD_SPLIT option for
the evaluation of spec.
- ${~spec}
-
Toggle the value of the
GLOB_SUBST option for the evaluation of spec. When
this option is set, any pattern characters resulting from the
substitution become eligible for file expansion and filename
generation.
If the colon is omitted from one of the above expressions containing a
colon, then the shell only checks whether name is set or not,
not whether it is null.
If the opening brace is directly followed by an opening parentheses the string up to the matching closing parentheses will be taken as a list of flags. Where arguments are valid, any character, or the matching pairs `(...)', `{...}', `[...]', or `<...>', may be used in place of the colon as delimiters. The following flags are supported:
- o
- Sort the resulting words in ascending order.
- O
- Sort the resulting words in descending order.
- i
- With o or O, sort case-independently.
- L
- Convert all letters in the result to lower case.
- U
- Convert all letters in the result to upper case.
- C
- Capitalize the resulting words.
- c
- With ${#name}, count the total number of characters in an
array, as if the elements were concatenated with spaces between
them.
- w
- With ${#name}, count words in arrays or strings; the
s flag may be used to set a word delimiter.
- l:expr::string1::string2:
-
Pad the resulting words on the left. Each word will be truncated if
required and placed in a field expr characters wide. The space
to the left will be filled with string1 (concatenated as often
as needed) or spaces if string1 is not given.
- If both
string1 and string2 are given, this string will be
placed exactly once directly to the left of the resulting word.
- r:expr::string1::string2:
-
As l..., but pad the words on the right.
- j:string:
-
Join the words of arrays together using string as a
separator. Note that this occurs before word splitting by the
SH_WORD_SPLIT option.
- s:string:
-
Force word splitting (see the option SH_WORD_SPLIT) at the
separator string. Splitting only occurs in places where an
array value is valid.
- S
-
(This and all remaining flags are used with the ${...#...} or
${...%...} forms): search substrings as well as beginnings or
ends.
- I:expr:
-
Search the expr'th match (where expr evaluates to a number).
- M
- Include the matched portion in the result.
- R
- Include the unmatched portion in the result (the Rest).
- B
- Include the index of the beginning of the match in the result.
- E
- Include the index of the end of the match in the result.
- N
- Include the length of the match in the result.
Mark D. Borges