Previous: , Up: Word Expansion   [Contents][Index]


10.4.6 Details of Variable Substitution

Part of ordinary shell syntax is the use of ‘$variable’ to substitute the value of a shell variable into a command. This is called variable substitution, and it is one part of doing word expansion.

There are two basic ways you can write a variable reference for substitution:

${variable}

If you write braces around the variable name, then it is completely unambiguous where the variable name ends. You can concatenate additional letters onto the end of the variable value by writing them immediately after the close brace. For example, ‘${foo}s’ expands into ‘tractors’.

$variable

If you do not put braces around the variable name, then the variable name consists of all the alphanumeric characters and underscores that follow the ‘$’. The next punctuation character ends the variable name. Thus, ‘$foo-bar’ refers to the variable foo and expands into ‘tractor-bar’.

When you use braces, you can also use various constructs to modify the value that is substituted, or test it in various ways.

${variable:-default}

Substitute the value of variable, but if that is empty or undefined, use default instead.

${variable:=default}

Substitute the value of variable, but if that is empty or undefined, use default instead and set the variable to default.

${variable:?message}

If variable is defined and not empty, substitute its value.

Otherwise, print message as an error message on the standard error stream, and consider word expansion a failure.

${variable:+replacement}

Substitute replacement, but only if variable is defined and nonempty. Otherwise, substitute nothing for this construct.

${#variable}

Substitute a numeral which expresses in base ten the number of characters in the value of variable. ‘${#foo}’ stands for ‘7’, because ‘tractor’ is seven characters.

These variants of variable substitution let you remove part of the variable’s value before substituting it. The prefix and suffix are not mere strings; they are wildcard patterns, just like the patterns that you use to match multiple file names. But in this context, they match against parts of the variable value rather than against file names.

${variable%%suffix}

Substitute the value of variable, but first discard from that variable any portion at the end that matches the pattern suffix.

If there is more than one alternative for how to match against suffix, this construct uses the longest possible match.

Thus, ‘${foo%%r*}’ substitutes ‘t’, because the largest match for ‘r*’ at the end of ‘tractor’ is ‘ractor’.

${variable%suffix}

Substitute the value of variable, but first discard from that variable any portion at the end that matches the pattern suffix.

If there is more than one alternative for how to match against suffix, this construct uses the shortest possible alternative.

Thus, ‘${foo%r*}’ substitutes ‘tracto’, because the shortest match for ‘r*’ at the end of ‘tractor’ is just ‘r’.

${variable##prefix}

Substitute the value of variable, but first discard from that variable any portion at the beginning that matches the pattern prefix.

If there is more than one alternative for how to match against prefix, this construct uses the longest possible match.

Thus, ‘${foo##*t}’ substitutes ‘or’, because the largest match for ‘*t’ at the beginning of ‘tractor’ is ‘tract’.

${variable#prefix}

Substitute the value of variable, but first discard from that variable any portion at the beginning that matches the pattern prefix.

If there is more than one alternative for how to match against prefix, this construct uses the shortest possible alternative.

Thus, ‘${foo#*t}’ substitutes ‘ractor’, because the shortest match for ‘*t’ at the beginning of ‘tractor’ is just ‘t’.


Previous: , Up: Word Expansion   [Contents][Index]