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


Substitutions

Substitutions provide a way of including text in a word or wordlist by describing how the text is to be discovered, rather than exactly what text is to be used. All substitutions (with one exception mentioned under Command Substitution ) are introduced by a dollar sign ($). This is generally followed by some text enclosed between two grouping symbols, either braces, parentheses or brackets, with the interpretation of the content depending on which grouping symbols are used. As Variable Substitution is very common, it is permitted in simple cases to omit the grouping symbol (in this case braces) and the extent of the substitution is determined based on the first character as follows, rather than simply up to the closing brace.

If this character is an alphabetic or underscore character, a maximal sequence of alphanumeric characters is taken; if it is a digit, a maximal sequence of digits, possibly followed by an asterisk or an at sign (`*' or `@') is taken; if it is the single character name of a parameter, just that one character is taken; otherwise the dollar sign is ignored, and not treated specially at all.

Substitution only occurs when the leading dollar sign is unquoted, or quoted only with double quotes. In either case, the text that is substituted has the same quoting attribute as that leading dollar sign.

Any quotation symbols within the grouping symbols are local to the grouping. e.g. the first double quote within the grouping symbols starts a section of quoted text, and does not terminate a quoted section begun outside the grouping.

Parameter and Variable Substitutions

Parameters and Variables are named values which can be included in a word or wordlist. The basic differences between variables and parameters are that the value of a variable may be changed directly while a parameter has a specific meaning, and its value may only be changed indirectly if at all; and that parameters are named by single special characters whereas variables are named by strings of alpha-numerics. [ FIX-ME what about underscores ] For more information on what parameters are defined, and the general use of variables, see Parameters and Variables below.

The simplest way to included the value of a named value (i.e. a parameter or variable) into a word is to prefix the name with a dollar sign ($), thus

$fred

will be replaced by the value of the variable FRED and

$+

will be replaced by the value of the parameter `+'.

The more general method of including named values is with substitutions of the form

  ${<name><modifier>}

where <modifier> is optional and is either a default modifier or a list of value modifiers preceded by a colon (:).

A default modifier (with one exception) only affects the substitution if the name is not associated with any value or, possibly, if the value associated is empty. Default modifiers have the form

[:][-+=?]< string>

If the leading colon if given, then the modifier has effect if the value of the name is empty or non-existent, otherwise it only has effect if the value is non-existent.

The next character controls what the modifier does.

`---'
This will simply substitute the given string instead of the named value.
`='
This substitutes the given value, but also assigns this value to the named variable. Note that this modifier is not permitted with parameters.
`?'
If this modifier takes effect, it causes an error and prints out the given string as a message.
`+'
This is the exceptional modifier in that it only takes effect if the value IS defined (or is non-null). The effect, like `---', is to substitute the given string instead of the named value.

In all cases, the string is subject to further substitution if, and only if, it is to be used.

Value modifiers modify the value before it is substituted. The different modifiers are applied in sequence, and the resulting string is substituted. If the value is a `hard-array' value (see Variables below), then the modifiers are applied independently to each element of the array, and a list of resultant values is substituted.

The possible modifiers, with their actions, are:

`#'
The value must be an array. It is replaced by the size of the array. Actually the highest index of an element which is not undefined. "' FIX-ME above is not clean
`n<base>'
Where <base> is a decimal integer, interprets the value as a number (see Arithmetic) and converts it to the base <base>.
`A'
Converts all lowercase alphabetic characters to uppercase.
`a'
Converts all uppercase alphabetic characters to lowercase.
`d'
Assumes the value is a pathname, and returns the directory part of that name. More precisely, it strips all trailing non-slash characters.
`b'
Assumes the value is a pathname, and returns the base-name of that name. Precisely, if there is a slash, the all characters up to and including the last slash are removed. If the resulting string is non-empty it is used, otherwise it is replaced by a period `.'.
`f'
Assumes the value is a pathname, and returns the filename part. If the name contains no characters other than slashes, then a period is appended, otherwise all trailing slashes are removed.
`p'
The shortest prefix of the value is returned. This is defined as all characters before the first period. "' after the last slash. "' If there are no slashes, all characters before the first period. If there are no periods, then all characters.
`P'
The longest prefix of the value is returned. "'If there is a period that has no slashes after it, the last such "'period and all subsequent characters are removed. That is, if there is a period, then all characters from the last period onwards are removed.
`s'
The shortest suffix is returned. That is, the longest prefix is removed.
`S'
The longest suffix is returned. That is, the shortest prefix is removed.
`ln'
The value is made to be exactly n characters wide, truncating on the right, or padding with spaces on the right as necessary (it is left justified).
`Lcn'
Same as for l except that the character c is used for padding.
`rn'
The value is right justified in a field of n characters. Spaces are added to, or characters are removed from, the left end to make it fit.
`Rcn'
As for r except that the character c is used for padding.
`cn'
Center in a field of n characters. Spaces are added to, or characters removed from, both ends as equally as possible to make the string the correct width.
`Ccn'
As for c except that the character c is used for padding.
`h[---]n'
Return the head of the string. That is the first n characters or (with the hyphen) all but the last n. If n is larger than the length of the string, it is reduced to that length.
`t[---]n'
The tail of the string, the last n characters, or all but first n, are returned.
`e'
The extent (length) of the string (as an integer) is returned.
`/pattern/replacement/'
If the value matches the pattern, it is replaced by the replacement otherwise it is left untouched. The pattern is of the same form as for file name expansion, except that slashes and leading periods are not significant, and the equals sign matches the same as an asterisk. Slashes may be included in the pattern or the replacement by quoting them differently to the delimiting slashes. i.e. if the first slash is quoted, the next two quoted slashes are delimiters, otherwise the next two unquoted slashes are delimiters. An unquoted equals sign in the replacement will be replaced by the string that the first unquoted equals in the pattern matched. If there are more than one equals signs in the pattern, then the strings that they match can be obtained by following an equals sign in the replacement with an unquoted digit. An equals sign or an asterisk matches the shortest possible string. Thus, if the value is "x+y+z", then the modifier
/=+=/=2+=1/
will produce the string "y+z+x".

Command Substitution

Command substitution allows for the inclusion of the output of a command into a word. Command substitution is invoked by enclosing a command in parentheses, and prefixing with a dollar sign, thus

$(command)

invokes command substitution of the command command. To provide backward compatibility with earlier shells, command substitution can also be performed by enclosing the command between back quotes (grave accents) as

`command`

The effect of command substitution is to run the command with standard output collected by the shell rather than being sent where standard output would normally go. The shell then strips off any trailing newline characters (leaving any newlines in the middle of the text alone) and replaces the word by this output. "' FIX-ME where will command substitution fail

In the general case, a new process is forked to run the command and a pipe is created between that process and the shell for collection of the output. If the command is a simple builtin command, with no I/O redirection "' FIX-ME what about named parameters? and is one of those marked as non-forking, then the command is run without the overhead of forking.

As a special case to provide often used functionality, if the command is a simple command with no actual command words, and with redirection of standard input as the only I/O redirection, then the file that standard input was redirected from is read and used as though it were the output of the command. Thus

$(< afile )

has the same effect as

$( cat afile)

but without the overhead of forking and running cat. "' what about $(| commands) and $( commands | )

Arithmetic Substitution

Arithmetic Substitution allows the value of an arithmetic expression to be included (in base 10) in a word list. Any text enclosed in brackets and preceded by a dollar sign is treated as an arithmetic expression (see Arithmetic) and the result is converted to base 10 and substituted. "' FIXME why no $[45+56:n6] for base six For example

echo $[i++]

will print the current value of I and increment its value.


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