Replaces all references to environment variables in
Source
by that
variable's definition (or the empty string, if an environment variable
is not defined) and returns the resulting string.
A Bash_Expander
uses a syntax very similar to that of the GNU
bash
shell. An environment variable has a name, which is an
identifier:
Ident = Alpha { Alpha | Digit | '_' }.
Alpha = 'A' .. 'Z' | 'a' ..'z'.
Digit = '0' .. '9'.
A simple reference to a variable has the form
$Identifier
and is replaced as a whole (including the '$' sign) by the variable's
value or by the empty string if no such variable is defined.
More interesting are the complex variable references, which have the
syntax
Value = any string, maybe containing environment
variable references.
Operator = :- | :+.
Reference = ${Var_Name[Operator[Value]]}.
Var_Name = Value | !Identifier.
In all forms, Variable_Name
can have one of three formats:
- An identifier: expands to the empty string if no such variable
is defined, and to the variable's value otherwise.
- A '!' and an identifier: expands the identifier as above, but
then takes the result of this expansion as the name of another
variable, which is then expanded. This is known as indirect
expansion, and is limited to one level of indirection only.
- Some arbitrary string that may contain embedded references to
variables: environment variable substitution is performed on the
whole thing, and the resulting value is taken to be the name of
a variable, which is then expanded. This recursive
expansion is unknown in
bash
>, and it is done
for as many levels as specified.
The semantics of these forms is as follows:
${Variable_Name}
- Is identical to the simple form of references
$Identifier
except that it also allows indirect and recursive expansion.
${Variable_Name:-Value}
- Is replaced by the result of
${Variable_Name}
unless that
result is empty, in which case it is replaced by the expansion
of Value
.
${Variable_Name:+Value}
- Is replaced by the expansion of
Value
if the result of
${Variable_Name}
is non-empty, or the empty string otherwise.
(:+
is the inverse of :-
.)
Indirect expansion using the '!
' character is supported only to keep
the syntax as close to the one used by bash
as possible. It is
actually superfluous and can be replaced by the more powerful (and, so
I think, simpler because more regular) recursive expansion:
"${!Some_Name}
" is identical to "${${Some_Name}}
" or
"${$Some_Name}
".
In all operators, the ':
' is actually optional. It appears that it
is optional in bash
(although the manual doesn't say so),
and I have therefore chosen to make it optional here, too.
To include a literal dollar sign '$
' in the result of the expansion
of Source
, escape it with a backslash and write "\$
". If, for some
reason, you want to have a backslash immediately before a variable
reference without escaping the dollar sign, escape the backslash by
writing two backslashes before the dollar. The sequence
"\\
" immediately followed by a variable reference is replaced by
a single backslash and the substitution of the reference.
Variable references that are not terminated properly are not replaced.
E.g. "${Var
" is returned unchanged.
A Bash_Expander
implements only the two operators '-
' and '+
'.
Other operators may be added in derived types.
Also, a Bash_Expander
has no special variables. To add those,
override Get
and/or Expand_Variable
as needed. In general, special
variables with names that are not identifiers will require overriding
of Expand_Variable
. Other special variables may be implemented by
overriding Get
only.