(libc.info.gz) Variable Substitution

Info Catalog (libc.info.gz) Tilde Expansion (libc.info.gz) Word Expansion
 
 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'.
 
Info Catalog (libc.info.gz) Tilde Expansion (libc.info.gz) Word Expansion
automatically generated by info2html