| <html lang="en"> |
| <head> |
| <title>Variable Substitution - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Word-Expansion.html#Word-Expansion" title="Word Expansion"> |
| <link rel="prev" href="Tilde-Expansion.html#Tilde-Expansion" title="Tilde Expansion"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Variable-Substitution"></a> |
| <p> |
| Previous: <a rel="previous" accesskey="p" href="Tilde-Expansion.html#Tilde-Expansion">Tilde Expansion</a>, |
| Up: <a rel="up" accesskey="u" href="Word-Expansion.html#Word-Expansion">Word Expansion</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">10.4.6 Details of Variable Substitution</h4> |
| |
| <p>Part of ordinary shell syntax is the use of ‘<samp><span class="samp">$</span><var>variable</var></samp>’ to |
| substitute the value of a shell variable into a command. This is called |
| <dfn>variable substitution</dfn>, and it is one part of doing word expansion. |
| |
| <p>There are two basic ways you can write a variable reference for |
| substitution: |
| |
| <dl> |
| <dt><code>${</code><var>variable</var><code>}</code><dd>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, ‘<samp><span class="samp">${foo}s</span></samp>’ |
| expands into ‘<samp><span class="samp">tractors</span></samp>’. |
| |
| <br><dt><code>$</code><var>variable</var><dd>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 ‘<samp><span class="samp">$</span></samp>’. The next punctuation character ends the variable |
| name. Thus, ‘<samp><span class="samp">$foo-bar</span></samp>’ refers to the variable <code>foo</code> and expands |
| into ‘<samp><span class="samp">tractor-bar</span></samp>’. |
| </dl> |
| |
| <p>When you use braces, you can also use various constructs to modify the |
| value that is substituted, or test it in various ways. |
| |
| <dl> |
| <dt><code>${</code><var>variable</var><code>:-</code><var>default</var><code>}</code><dd>Substitute the value of <var>variable</var>, but if that is empty or |
| undefined, use <var>default</var> instead. |
| |
| <br><dt><code>${</code><var>variable</var><code>:=</code><var>default</var><code>}</code><dd>Substitute the value of <var>variable</var>, but if that is empty or |
| undefined, use <var>default</var> instead and set the variable to |
| <var>default</var>. |
| |
| <br><dt><code>${</code><var>variable</var><code>:?</code><var>message</var><code>}</code><dd>If <var>variable</var> is defined and not empty, substitute its value. |
| |
| <p>Otherwise, print <var>message</var> as an error message on the standard error |
| stream, and consider word expansion a failure. |
| |
| <!-- ??? How does wordexp report such an error? --> |
| <!-- WRDE_BADVAL is returned. --> |
| <br><dt><code>${</code><var>variable</var><code>:+</code><var>replacement</var><code>}</code><dd>Substitute <var>replacement</var>, but only if <var>variable</var> is defined and |
| nonempty. Otherwise, substitute nothing for this construct. |
| </dl> |
| |
| <dl> |
| <dt><code>${#</code><var>variable</var><code>}</code><dd>Substitute a numeral which expresses in base ten the number of |
| characters in the value of <var>variable</var>. ‘<samp><span class="samp">${#foo}</span></samp>’ stands for |
| ‘<samp><span class="samp">7</span></samp>’, because ‘<samp><span class="samp">tractor</span></samp>’ is seven characters. |
| </dl> |
| |
| <p>These variants of variable substitution let you remove part of the |
| variable's value before substituting it. The <var>prefix</var> and |
| <var>suffix</var> 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. |
| |
| <dl> |
| <dt><code>${</code><var>variable</var><code>%%</code><var>suffix</var><code>}</code><dd>Substitute the value of <var>variable</var>, but first discard from that |
| variable any portion at the end that matches the pattern <var>suffix</var>. |
| |
| <p>If there is more than one alternative for how to match against |
| <var>suffix</var>, this construct uses the longest possible match. |
| |
| <p>Thus, ‘<samp><span class="samp">${foo%%r*}</span></samp>’ substitutes ‘<samp><span class="samp">t</span></samp>’, because the largest |
| match for ‘<samp><span class="samp">r*</span></samp>’ at the end of ‘<samp><span class="samp">tractor</span></samp>’ is ‘<samp><span class="samp">ractor</span></samp>’. |
| |
| <br><dt><code>${</code><var>variable</var><code>%</code><var>suffix</var><code>}</code><dd>Substitute the value of <var>variable</var>, but first discard from that |
| variable any portion at the end that matches the pattern <var>suffix</var>. |
| |
| <p>If there is more than one alternative for how to match against |
| <var>suffix</var>, this construct uses the shortest possible alternative. |
| |
| <p>Thus, ‘<samp><span class="samp">${foo%r*}</span></samp>’ substitutes ‘<samp><span class="samp">tracto</span></samp>’, because the shortest |
| match for ‘<samp><span class="samp">r*</span></samp>’ at the end of ‘<samp><span class="samp">tractor</span></samp>’ is just ‘<samp><span class="samp">r</span></samp>’. |
| |
| <br><dt><code>${</code><var>variable</var><code>##</code><var>prefix</var><code>}</code><dd>Substitute the value of <var>variable</var>, but first discard from that |
| variable any portion at the beginning that matches the pattern <var>prefix</var>. |
| |
| <p>If there is more than one alternative for how to match against |
| <var>prefix</var>, this construct uses the longest possible match. |
| |
| <p>Thus, ‘<samp><span class="samp">${foo##*t}</span></samp>’ substitutes ‘<samp><span class="samp">or</span></samp>’, because the largest |
| match for ‘<samp><span class="samp">*t</span></samp>’ at the beginning of ‘<samp><span class="samp">tractor</span></samp>’ is ‘<samp><span class="samp">tract</span></samp>’. |
| |
| <br><dt><code>${</code><var>variable</var><code>#</code><var>prefix</var><code>}</code><dd>Substitute the value of <var>variable</var>, but first discard from that |
| variable any portion at the beginning that matches the pattern <var>prefix</var>. |
| |
| <p>If there is more than one alternative for how to match against |
| <var>prefix</var>, this construct uses the shortest possible alternative. |
| |
| <p>Thus, ‘<samp><span class="samp">${foo#*t}</span></samp>’ substitutes ‘<samp><span class="samp">ractor</span></samp>’, because the shortest |
| match for ‘<samp><span class="samp">*t</span></samp>’ at the beginning of ‘<samp><span class="samp">tractor</span></samp>’ is just ‘<samp><span class="samp">t</span></samp>’. |
| |
| </dl> |
| |
| </body></html> |
| |