⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 variables.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 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&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Variables: assignment and substitution   4&nbsp;   5&nbsp;a=375   6&nbsp;hello=$a   7&nbsp;   8&nbsp;#-------------------------------------------------------------------------   9&nbsp;# No space permitted on either side of = sign when initializing variables.  10&nbsp;# What happens if there is a space?  11&nbsp;  12&nbsp;#  If "VARIABLE =value",  13&nbsp;#              ^  14&nbsp;#+ script tries to run "VARIABLE" command with one argument, "=value".  15&nbsp;  16&nbsp;#  If "VARIABLE= value",  17&nbsp;#               ^  18&nbsp;#+ script tries to run "value" command with  19&nbsp;#+ the environmental variable "VARIABLE" set to "".  20&nbsp;#-------------------------------------------------------------------------  21&nbsp;  22&nbsp;  23&nbsp;echo hello    # Not a variable reference, just the string "hello".  24&nbsp;  25&nbsp;echo $hello  26&nbsp;echo ${hello} # Identical to above.  27&nbsp;  28&nbsp;echo "$hello"  29&nbsp;echo "${hello}"  30&nbsp;  31&nbsp;echo  32&nbsp;  33&nbsp;hello="A B  C   D"  34&nbsp;echo $hello   # A B C D  35&nbsp;echo "$hello" # A B  C   D  36&nbsp;# As you see, echo $hello   and   echo "$hello"   give different results.  37&nbsp;#                                      ^      ^  38&nbsp;# Quoting a variable preserves whitespace.  39&nbsp;  40&nbsp;echo  41&nbsp;  42&nbsp;echo '$hello'  # $hello  43&nbsp;#    ^      ^  44&nbsp;#  Variable referencing disabled by single quotes,  45&nbsp;#+ which causes the "$" to be interpreted literally.  46&nbsp;  47&nbsp;# Notice the effect of different types of quoting.  48&nbsp;  49&nbsp;  50&nbsp;hello=    # Setting it to a null value.  51&nbsp;echo "\$hello (null value) = $hello"  52&nbsp;#  Note that setting a variable to a null value is not the same as  53&nbsp;#+ unsetting it, although the end result is the same (see below).  54&nbsp;  55&nbsp;# --------------------------------------------------------------  56&nbsp;  57&nbsp;#  It is permissible to set multiple variables on the same line,  58&nbsp;#+ if separated by white space.  59&nbsp;#  Caution, this may reduce legibility, and may not be portable.  60&nbsp;  61&nbsp;var1=21  var2=22  var3=$V3  62&nbsp;echo  63&nbsp;echo "var1=$var1   var2=$var2   var3=$var3"  64&nbsp;  65&nbsp;# May cause problems with older versions of "sh".  66&nbsp;  67&nbsp;# --------------------------------------------------------------  68&nbsp;  69&nbsp;echo; echo  70&nbsp;  71&nbsp;numbers="one two three"  72&nbsp;#           ^   ^  73&nbsp;other_numbers="1 2 3"  74&nbsp;#               ^ ^  75&nbsp;#  If there is whitespace embedded within a variable,  76&nbsp;#+ then quotes are necessary.  77&nbsp;echo "numbers = $numbers"  78&nbsp;echo "other_numbers = $other_numbers"   # other_numbers = 1 2 3  79&nbsp;echo  80&nbsp;  81&nbsp;echo "uninitialized_variable = $uninitialized_variable"  82&nbsp;# Uninitialized variable has null value (no value at all).  83&nbsp;uninitialized_variable=   #  Declaring, but not initializing it --  84&nbsp;                          #+ same as setting it to a null value, as above.  85&nbsp;echo "uninitialized_variable = $uninitialized_variable"  86&nbsp;                          # It still has a null value.  87&nbsp;  88&nbsp;uninitialized_variable=23       # Set it.  89&nbsp;unset uninitialized_variable    # Unset it.  90&nbsp;echo "uninitialized_variable = $uninitialized_variable"  91&nbsp;                                # It still has a null value.  92&nbsp;echo  93&nbsp;  94&nbsp;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&nbsp;echo "$uninitialized"                                # (blank line)   2&nbsp;let "uninitialized += 5"                             # Add 5 to it.   3&nbsp;echo "$uninitialized"                                # 5   4&nbsp;   5&nbsp;#  Conclusion:   6&nbsp;#  An uninitialized variable has no value, however   7&nbsp;#+ it acts as if it were 0 in an arithmetic operation.   8&nbsp;#  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 + -