Next: , Previous: , Up: Top   [Contents][Index]


2 Variable references

A variable reference has the form ‘$variable’ or ‘${variable}’, where variable is the environment variable name. The two forms are entirely equivalent. The form with curly braces is normally used if the variable name is immediately followed by an alphanumeric symbol, which will otherwise be considered part of it. This form also allows for specifying the action to take if the variable is undefined or expands to an empty value:

${variable:-word}

Use Default Values. If variable is unset or null, the expansion of word is substituted. Otherwise, the value of variable is substituted.

${variable:=word}

Assign Default Values. If variable is unset or null, the expansion of word is assigned to variable. The value of variable is then substituted.

${variable:?word}

Display Error if Null or Unset. If variable is null or unset, the expansion of word (or a message to that effect if word is not present) is output to the current logging channel. Otherwise, the value of variable is substituted.

${variable:+word}

Use Alternate Value. If variable is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

${variable:|word1|word2}

Display Alternative Values. Unless variable is null or unset, the expansion of word1 is substituted, otherwise the expansion of word2 is substituted.

In the constructs above the colon can be omitted, resulting in a test only for a variable that is unset.

In any expansion form, the word part is treated exactly as in Bourne shell: it can contain variable expansions, command substitutions, single and double-quoted parts. The latter two are interpreted as follows: the quotes are removed, the text between double quotes is subject to variable expansion and command substitution (see below), whereas the test between single quotes is not. Within double quoted part, a backslash can be used to escape the indirection character (‘$’), double and single quote character and backslash itself.

Several command line options control the expansion process. Unless special reference forms are used, a reference to an undefined variable expands to an empty string. The -r option changes that. When given that option xenv will retain references to undefined variables in the produced output.

The -u option instructs xenv to treat a reference to an undefined variable as error.

If the input text can contain a considerable number of ‘$’ characters, which can be misinterpreted as variable references, it is possible to change variable reference syntax to use a single meta-variable instead. For example, when invoked as

xenv -e ENV

xenv will recognize $ENV{name} as the environment variable reference. The constructs $name, ${name} and others will be reproduced verbatim. This feature is described in detail in Meta-variable.


Next: , Previous: , Up: Top   [Contents][Index]