📄 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.57"><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"><TABLEWIDTH="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">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="varassignment.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="VARIABLES">Chapter 4. Introduction to Variables and Parameters</A></H1><P><ICLASS="FIRSTTERM">Variables</I> are how programming and scripting languages represent data. They appear in arithmetic operations and manipulation of quantities, in string parsing, and they are indispensable for working in the abstract with symbols -- tokens that represent something else. A variable is nothing more than a <ICLASS="EMPHASIS">label</I> assigned to a location or set of locations in computer memory holding an item of data.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="VARSUBN">4.1. Variable Substitution</A></H1><P>The <ICLASS="EMPHASIS">name</I> of a variable is a placeholder for its <ICLASS="EMPHASIS">value</I>, the data it holds. Referencing its value is called <ICLASS="EMPHASIS">variable substitution</I>.</P><DIVCLASS="VARIABLELIST"><DL><DT><SPANCLASS="TOKEN">$</SPAN></DT><DD><P>Let us carefully distinguish between the <ICLASS="EMPHASIS">name</I> of a variable and its <ICLASS="EMPHASIS">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="EMPHASIS">value</I>, the data item it contains.</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="EMPHASIS">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 <ICLASS="EMPHASIS">var1=27</I>), in a <AHREF="internal.html#READREF">read</A> statement, and at the head of a loop (<ICLASS="EMPHASIS">for var2 in 1 2 3</I>).</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><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 3 # Variables: assignment and substitution 4 5 a=375 6 hello=$a 7 8 #------------------------------------------------------------------------- 9 # No space permitted on either side of = sign when initializing variables. 10 # What happens if there is a space? 11 12 # If "VARIABLE =value", 13 # ^ 14 #+ script tries to run "VARIABLE" command with one argument, "=value". 15 16 # If "VARIABLE= value", 17 # ^ 18 #+ script tries to run "value" command with 19 #+ the environmental variable "VARIABLE" set to "". 20 #------------------------------------------------------------------------- 21 22 23 echo hello # Not a variable reference, just the string "hello". 24 25 echo $hello 26 echo ${hello} # Identical to above. 27 28 echo "$hello" 29 echo "${hello}" 30 31 echo 32 33 hello="A B C D" 34 echo $hello # A B C D 35 echo "$hello" # A B C D 36 # As you see, echo $hello and echo "$hello" give different results. 37 # ^ ^ 38 # Quoting a variable preserves whitespace. 39 40 echo 41 42 echo '$hello' # $hello 43 # ^ ^ 44 # Variable referencing disabled by single quotes, 45 #+ which causes the "$" to be interpreted literally. 46 47 # Notice the effect of different types of quoting. 48 49 50 hello= # Setting it to a null value. 51 echo "\$hello (null value) = $hello" 52 # Note that setting a variable to a null value is not the same as 53 #+ unsetting it, although the end result is the same (see below). 54 55 # -------------------------------------------------------------- 56 57 # It is permissible to set multiple variables on the same line, 58 #+ if separated by white space. 59 # Caution, this may reduce legibility, and may not be portable. 60 61 var1=21 var2=22 var3=$V3 62 echo 63 echo "var1=$var1 var2=$var2 var3=$var3" 64 65 # May cause problems with older versions of "sh". 66 67 # -------------------------------------------------------------- 68 69 echo; echo 70 71 numbers="one two three" 72 # ^ ^ 73 other_numbers="1 2 3" 74 # ^ ^ 75 # If there is whitespace embedded within a variable, 76 #+ then quotes are necessary. 77 echo "numbers = $numbers" 78 echo "other_numbers = $other_numbers" # other_numbers = 1 2 3 79 echo 80 81 echo "uninitialized_variable = $uninitialized_variable" 82 # Uninitialized variable has null value (no value at all). 83 uninitialized_variable= # Declaring, but not initializing it -- 84 #+ same as setting it to a null value, as above. 85 echo "uninitialized_variable = $uninitialized_variable" 86 # It still has a null value. 87 88 uninitialized_variable=23 # Set it. 89 unset uninitialized_variable # Unset it. 90 echo "uninitialized_variable = $uninitialized_variable" 91 # It still has a null value. 92 echo 93 94 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>An uninitialized variable has a <SPANCLASS="QUOTE">"null"</SPAN> value - no assigned value at all (not zero!). Using a variable before assigning a value to it will usually 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, however 7 #+ it acts as if it were 0 in an arithmetic operation. 8 # This is undocumented (and probably non-portable) behavior.</PRE></TD></TR></TABLE> See also <AHREF="internal.html#SELFSOURCE">Example 11-21</A>.</P></TD></TR></TABLE></DIV></DD></DL></DIV></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="special-chars.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="varassignment.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Special Characters</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part2.html">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 + -