📄 variables.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Introduction to Variables and Parameters</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Basics"HREF="part2.html"><LINKREL="PREVIOUS"TITLE="Special Characters"HREF="special-chars.html"><LINKREL="NEXT"TITLE="Variable Assignment"HREF="varassignment.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="special-chars.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="varassignment.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="VARIABLES"></A>Chapter 4. Introduction to Variables and Parameters</H1><P><ICLASS="FIRSTTERM">Variables</I> are how programming and scripting languages represent data. A variable is nothing more than a <ICLASS="FIRSTTERM">label</I>, a name assigned to a location or set of locations in computer memory holding an item of data.</P><P>Variables appear in arithmetic operations and manipulation of quantities, and in string parsing.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="VARSUBN"></A>4.1. Variable Substitution</H1><P>The <ICLASS="FIRSTTERM">name</I> of a variable is a placeholder for its <ICLASS="FIRSTTERM">value</I>, the data it holds. Referencing its value is called <ICLASS="FIRSTTERM">variable substitution</I>.</P><DIVCLASS="VARIABLELIST"><DL><DT><SPANCLASS="TOKEN">$</SPAN></DT><DD><P><ANAME="VARNAMEVAL"></A></P><P>Let us carefully distinguish between the <ICLASS="FIRSTTERM">name</I> of a variable and its <ICLASS="FIRSTTERM">value</I>. If <TTCLASS="USERINPUT"><B>variable1</B></TT> is the name of a variable, then <TTCLASS="USERINPUT"><B>$variable1</B></TT> is a reference to its <ICLASS="FIRSTTERM">value</I>, the data item it contains. <ANAME="AEN2002"HREF="#FTN.AEN2002">[1]</A> </P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>variable=23</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo variable</B></TT> <TTCLASS="COMPUTEROUTPUT">variable</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $variable</B></TT> <TTCLASS="COMPUTEROUTPUT">23</TT></PRE></TD></TR></TABLE> </P><P>The only time a variable appears <SPANCLASS="QUOTE">"naked"</SPAN> -- without the <SPANCLASS="TOKEN">$</SPAN> prefix -- is when declared or assigned, when <ICLASS="FIRSTTERM">unset</I>, when <AHREF="internal.html#EXPORTREF">exported</A>, or in the special case of a variable representing a <AHREF="debugging.html#SIGNALD">signal</A> (see <AHREF="debugging.html#EX76">Example 29-5</A>). Assignment may be with an <SPANCLASS="TOKEN">=</SPAN> (as in <TTCLASS="PARAMETER"><I>var1=27</I></TT>), in a <AHREF="internal.html#READREF">read</A> statement, and at the head of a loop (<TTCLASS="PARAMETER"><I>for var2 in 1 2 3</I></TT>).</P><P><ANAME="DBLQUO"></A>Enclosing a referenced value in <ICLASS="FIRSTTERM">double quotes</I> (<SPANCLASS="TOKEN">" "</SPAN>) does not interfere with variable substitution. This is called <ICLASS="FIRSTTERM">partial quoting</I>, sometimes referred to as <SPANCLASS="QUOTE">"weak quoting."</SPAN> <ANAME="SNGLQUO"></A>Using single quotes (<SPANCLASS="TOKEN">' '</SPAN>) causes the variable name to be used literally, and no substitution will take place. This is <ICLASS="FIRSTTERM">full quoting</I>, sometimes referred to as <SPANCLASS="QUOTE">"strong quoting."</SPAN> See <AHREF="quoting.html">Chapter 5</A> for a detailed discussion.</P><P>Note that <TTCLASS="USERINPUT"><B>$variable</B></TT> is actually a simplified alternate form of <TTCLASS="USERINPUT"><B>${variable}</B></TT>. In contexts where the <TTCLASS="USERINPUT"><B>$variable</B></TT> syntax causes an error, the longer form may work (see <AHREF="parameter-substitution.html">Section 9.3</A>, below).</P><P><ANAME="VARUNSETTING"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX9"></A><P><B>Example 4-1. Variable assignment and substitution</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # ex9.sh 3 4 # Variables: assignment and substitution 5 6 a=375 7 hello=$a 8 9 #------------------------------------------------------------------------- 10 # No space permitted on either side of = sign when initializing variables. 11 # What happens if there is a space? 12 13 # "VARIABLE =value" 14 # ^ 15 #% Script tries to run "VARIABLE" command with one argument, "=value". 16 17 # "VARIABLE= value" 18 # ^ 19 #% Script tries to run "value" command with 20 #+ the environmental variable "VARIABLE" set to "". 21 #------------------------------------------------------------------------- 22 23 24 echo hello # hello 25 # Not a variable reference, just the string "hello" . . . 26 27 echo $hello # 375 28 # ^ This *is* a variable reference. 29 echo ${hello} # 375 30 # Also a variable reference, as above. 31 32 # Quoting . . . 33 echo "$hello" # 375 34 echo "${hello}" # 375 35 36 echo 37 38 hello="A B C D" 39 echo $hello # A B C D 40 echo "$hello" # A B C D 41 # As you see, echo $hello and echo "$hello" give different results. 42 # Why? 43 # ======================================= 44 # Quoting a variable preserves whitespace. 45 # ======================================= 46 47 echo 48 49 echo '$hello' # $hello 50 # ^ ^ 51 # Variable referencing disabled (escaped) by single quotes, 52 #+ which causes the "$" to be interpreted literally. 53 54 # Notice the effect of different types of quoting. 55 56 57 hello= # Setting it to a null value. 58 echo "\$hello (null value) = $hello" 59 # Note that setting a variable to a null value is not the same as 60 #+ unsetting it, although the end result is the same (see below). 61 62 # -------------------------------------------------------------- 63 64 # It is permissible to set multiple variables on the same line, 65 #+ if separated by white space. 66 # Caution, this may reduce legibility, and may not be portable. 67 68 var1=21 var2=22 var3=$V3 69 echo 70 echo "var1=$var1 var2=$var2 var3=$var3" 71 72 # May cause problems with older versions of "sh" . . . 73 74 # -------------------------------------------------------------- 75 76 echo; echo 77 78 numbers="one two three" 79 # ^ ^ 80 other_numbers="1 2 3" 81 # ^ ^ 82 # If there is whitespace embedded within a variable, 83 #+ then quotes are necessary. 84 # other_numbers=1 2 3 # Gives an error message. 85 echo "numbers = $numbers" 86 echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 87 # Escaping the whitespace also works. 88 mixed_bag=2\ ---\ Whatever 89 # ^ ^ Space after escape (\). 90 91 echo "$mixed_bag" # 2 --- Whatever 92 93 echo; echo 94 95 echo "uninitialized_variable = $uninitialized_variable" 96 # Uninitialized variable has null value (no value at all!). 97 uninitialized_variable= # Declaring, but not initializing it -- 98 #+ same as setting it to a null value, as above. 99 echo "uninitialized_variable = $uninitialized_variable" 100 # It still has a null value. 101 102 uninitialized_variable=23 # Set it. 103 unset uninitialized_variable # Unset it. 104 echo "uninitialized_variable = $uninitialized_variable" 105 # It still has a null value. 106 echo 107 108 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><ANAME="UNINITVAR1"></A></P><P>An uninitialized variable has a <SPANCLASS="QUOTE">"null"</SPAN> value -- no assigned value at all (<SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> zero!). Using a variable before assigning a value to it may cause problems.</P><P>It is nevertheless possible to perform arithmetic operations on an uninitialized variable. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo "$uninitialized" # (blank line) 2 let "uninitialized += 5" # Add 5 to it. 3 echo "$uninitialized" # 5 4 5 # Conclusion: 6 # An uninitialized variable has no value, 7 #+ however it acts as if it were 0 in an arithmetic operation. 8 # This is undocumented (and probably non-portable) behavior, 9 #+ and should not be used in a script.</PRE></TD></TR></TABLE> See also <AHREF="internal.html#SELFSOURCE">Example 14-23</A>.</P></TD></TR></TABLE></DIV></DD></DL></DIV></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2002"HREF="variables.html#AEN2002">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="LVALUEREF"></A>Technically, the <ICLASS="FIRSTTERM">name</I> of a variable is called an <ICLASS="FIRSTTERM">lvalue</I>, meaning that it appears on the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">left</I></SPAN> side of an assignment statment, as in <TTCLASS="USERINPUT"><B>VARIABLE=23</B></TT>. A variable's <ICLASS="FIRSTTERM">value</I> is an <ICLASS="FIRSTTERM">rvalue</I>, meaning that it appears on the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">right</I></SPAN> side of an assignment statement, as in <TTCLASS="USERINPUT"><B>VAR2=$VARIABLE</B></TT>.</P><P><ANAME="POINTERREF"></A>A variable's <ICLASS="FIRSTTERM">name</I> is, in fact, a <ICLASS="FIRSTTERM">reference</I>, a <ICLASS="FIRSTTERM">pointer</I> to the memory location(s) where the actual data associated with that variable is kept.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="special-chars.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="varassignment.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Special Characters</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Variable Assignment</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -