Go to the first, previous, next, last section, table of contents.


Expansion items

The following items are recognized in expanded strings. White space may be used between sub-items that are keywords or sub-strings enclosed in braces inside an outer set of braces, to improve readability.


$<variable name> or ${<variable name>}

Substitute the contents of the named variable; the latter form can be used to separate the name from subsequent alphanumeric characters. The names of the variables are given in section """Expansion variables" below. If the name of a non-existent variable is given, the expansion fails.


$header_<header name>: or $h_<header name>:

Substitute the contents of the named message header, for example


$header_reply-to:

This particular expansion is intended mainly for use in filter files. The header names follow the syntax of RFC 822, which states that they may contain any printing characters except space and colon. Consequently, curly brackets do not terminate header names. Upper-case and lower-case letters are synonymous in header names. If the following character is white space, the terminating colon may be omitted. The white space is included in the expanded string. If the message does not contain the given header, the expansion item is replaced by an empty string. (See the `def' condition in section """Expansion conditions" for a means of testing for the existence of a header.) If there is more than one header with the same name, they are all concatenated to form the substitution string, with a newline character between each of them.


${<op>:<string>}

The string is first itself expanded, and then the operation specified by <op> is applied to it. A list of operators is given in section """Expansion operators" below. The string starts with the first character after the colon, which may be leading white space.


${if <condition> {<string1>}{<string2>}}

If <condition> is true, <string1> is expanded and replaces the whole item; otherwise <string2> is used. The second string need not be present; if it is not and the condition is not true, the item is replaced with nothing. Alternatively, the word `fail' may be present instead of the second string (without any curly brackets). In this case, the expansion fails if the condition is not true. The available conditions are described in section """Expansion conditions" below.


${lookup{<key>} <search type> {<file>} {<string1>} {<string2>}}


${lookup <search type> {<query>} {<string1>} {<string2>}}

These items specify data lookups in files and databases, as discussed in chapter "". The first form is used for single-key lookups, and the second is used for query-style lookups. The <key>, <file>, and <query> strings are expanded before use.

If the lookup succeeds, then <string1> is expanded and replaces the entire item. During its expansion, a variable called `$value' is available, containing the data returned by the file lookup. If the lookup fails, <string2> is expanded and replaces the entire item. It may be omitted, in which case the replacement is null.

For single-key lookups, the string `partial-' is permitted to precede the search type in order to do partial matching, and * or *@ may follow a search type to request default lookups if the key does not match (see sections "" and "").

If a partial search is used, the variables `$1' and `$2' contain the wild and non-wild parts of the key during the expansion of the replacement text. They return to their previous values at the end of the lookup item.

Instead of {<string2>} the word `fail' can appear, and in this case, if the lookup fails, the entire string expansion fails in a way that can be detected by the caller. The consequences of this depend on the circumstances.

This example looks up the postmaster alias in the conventional alias file.


${lookup {postmaster} lsearch {/etc/aliases} {$value}}

This example uses NIS+ to look up the full name of the user corresponding to the local part of an address, failing the expansion if it is not found.


"${lookup nisplus {[name=$local_part],passwd.org_dir:gcos} \
  {$value}fail}"

${lookup{<key:subkey>} <search type> {<file>} {<string1>} {<string2>}}

This searches for <key> in the file as described above for single-key lookups; if it succeeds, it extracts from the data a subfield which is identified by the <subkey>. The data related to the main key must be of the form:


<subkey1> = <value1>  <subkey2> = <value2> ...

where the equals signs are optional. If any of the values contain white space, they must be enclosed in double quotes, and any values that are enclosed in double quotes are subject to escape processing as described in section "Format of filter files". For example, if a line in a linearly searched file contains


alice: uid=1984 gid=2001

then expanding the string


${lookup{alice:uid}lsearch{<file name>}{$value}}

yields the string `1984'. If the subkey is not found in <string1>, then <string2>, if present, is expanded and replaces the entire item. Otherwise the replacement is null.


${extract{<key>} {<string>}}

The key and the string are first expanded. Then the subfield identified by the key is extracted from the string, exactly as just described for `lookup' items with subkeys. If the key is not found in the string, the item is replaced by nothing.


${extract{<number>} {<separators>} {<string>}}

This is distinguished from the above form of `extract' by having three rather than two arguments. It extracts from the string the field whose number is given as the first argument. The first field is numbered one. If the number is negative or greater than the number of fields in the string, the result is empty; if it is zero the entire string is returned. The fields in the string are separated by any one of the characters in the separator string. For example:


${extract{3}{:}{exim:x:42:99:& Mailer::/bin/bash}}

yields `42'. Two successive separators mean that the field between them is empty (for example, the sixth field above). If the first argument is not numeric, the expansion fails.


Go to the first, previous, next, last section, table of contents.