old-www/HOWTO/Bash-Prompt-HOWTO/x206.html

230 lines
3.2 KiB
HTML

<HTML
><HEAD
><TITLE
>Quotes and Special Characters</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Bash Prompt HOWTO"
HREF="index.html"><LINK
REL="UP"
TITLE="Bash Programming and Shell Scripts"
HREF="c189.html"><LINK
REL="PREVIOUS"
TITLE="Variables"
HREF="x191.html"><LINK
REL="NEXT"
TITLE="Command Substitution"
HREF="x219.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Bash Prompt HOWTO: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x191.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 3. Bash Programming and Shell Scripts</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x219.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN206"
></A
>3.2. Quotes and Special Characters</H1
><P
>If you wish to include a special character in a variable, you will have to
quote it differently:</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>&#62; newvar=$testvar
&#62; echo $newvar
5
&#62; newvar="$testvar"
&#62; echo $newvar
5
&#62; newvar='$testvar'
&#62; echo $newvar
$testvar
&#62; newvar=\$testvar
&#62; echo $newvar
$testvar
&#62;</PRE
></FONT
></TD
></TR
></TABLE
><P
>The dollar sign isn't the only character that's special to the Bash shell,
but it's a simple example. An interesting step we can take to make use of
assigning a variable name to another variable name is to use
<TT
CLASS="USERINPUT"
><B
>eval</B
></TT
> to dereference the stored variable name:&#13;</P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>&#62; echo $testvar
5
&#62; echo $newvar
$testvar
&#62; eval echo $newvar
5
&#62; </PRE
></FONT
></TD
></TR
></TABLE
><P
>Normally, the shell does only one round of substitutions on the expression
it is evaluating: if you say <TT
CLASS="USERINPUT"
><B
>echo $newvar</B
></TT
> the shell
will only go so far as to determine that <TT
CLASS="VARNAME"
>$newvar</TT
> is
equal to the text string <TT
CLASS="VARNAME"
>$testvar</TT
>, it won't evaluate
what <TT
CLASS="VARNAME"
>$testvar</TT
> is equal to. <TT
CLASS="USERINPUT"
><B
>eval</B
></TT
>
forces that evaluation.&#13;</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x191.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x219.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Variables</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c189.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Command Substitution</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>