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

📄 declareref.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Typing variables: declare or	  typeset</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Variables Revisited"HREF="variables2.html"><LINKREL="PREVIOUS"TITLE="Parameter Substitution"HREF="parameter-substitution.html"><LINKREL="NEXT"TITLE="Indirect References"HREF="ivr.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="SECT1"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="parameter-substitution.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Variables Revisited</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="ivr.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="DECLAREREF"></A>9.4. Typing variables: <BCLASS="COMMAND">declare</B> or	  <BCLASS="COMMAND">typeset</B></H1><P><ANAME="DECLARE1REF"></A></P><P>The <ICLASS="FIRSTTERM">declare</I> or	  <ICLASS="FIRSTTERM">typeset</I> <AHREF="internal.html#BUILTINREF">builtins</A>, which are exact synonyms,	  permit modifying the properties of variables. This is	  a very weak form of the <ICLASS="FIRSTTERM">typing</I>	    <ANAME="AEN5568"HREF="#FTN.AEN5568">[1]</A>	  available in certain programming languages. The	  <ICLASS="FIRSTTERM">declare</I> command is specific to version	  2 or later of Bash. The <ICLASS="FIRSTTERM">typeset</I> command	  also works in ksh scripts.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="DECLAREOPSREF1"></A>declare/typeset options</B></P><DL><DT><SPANCLASS="TOKEN">-r</SPAN> <TTCLASS="REPLACEABLE"><I>readonly</I></TT></DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;declare -r var1</PRE></TD></TR></TABLE></P><P>(<TTCLASS="USERINPUT"><B>declare -r var1</B></TT> works the same as		<TTCLASS="USERINPUT"><B>readonly var1</B></TT>)</P><P>This is the rough equivalent of the C		<BCLASS="COMMAND">const</B> type qualifier. An 		attempt to change the value of a readonly variable fails with an 		error message.</P></DD><DT><SPANCLASS="TOKEN">-i</SPAN> <TTCLASS="REPLACEABLE"><I>integer</I></TT></DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;declare -i number   2&nbsp;# The script will treat subsequent occurrences of "number" as an integer.		   3&nbsp;   4&nbsp;number=3   5&nbsp;echo "Number = $number"     # Number = 3   6&nbsp;   7&nbsp;number=three   8&nbsp;echo "Number = $number"     # Number = 0   9&nbsp;# Tries to evaluate the string "three" as an integer.</PRE></TD></TR></TABLE></P><P>Certain arithmetic operations are permitted		for declared integer variables without the need		for <AHREF="moreadv.html#EXPRREF">expr</A> or <AHREF="internal.html#LETREF">let</A>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;n=6/3   2&nbsp;echo "n = $n"       # n = 6/3   3&nbsp;   4&nbsp;declare -i n   5&nbsp;n=6/3   6&nbsp;echo "n = $n"       # n = 2</PRE></TD></TR></TABLE></P></DD><DT><ANAME="ARRAYDECLARE"></A><SPANCLASS="TOKEN">-a</SPAN>	    <TTCLASS="REPLACEABLE"><I>array</I></TT></DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;declare -a indices</PRE></TD></TR></TABLE></P><P>The variable <TTCLASS="PARAMETER"><I>indices</I></TT> will be treated as		an <AHREF="arrays.html#ARRAYREF">array</A>.</P></DD><DT><SPANCLASS="TOKEN">-f</SPAN> <TTCLASS="REPLACEABLE"><I>function(s)</I></TT></DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;declare -f</PRE></TD></TR></TABLE></P><P>A <TTCLASS="USERINPUT"><B>declare -f</B></TT> line with no		arguments in a script causes a listing of all the		<AHREF="functions.html#FUNCTIONREF">functions</A> previously		defined in that script.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;declare -f function_name</PRE></TD></TR></TABLE></P><P>A <TTCLASS="USERINPUT"><B>declare -f function_name</B></TT>		in a script lists just the function named.</P></DD><DT><SPANCLASS="TOKEN">-x</SPAN> <AHREF="internal.html#EXPORTREF">export</A></DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;declare -x var3</PRE></TD></TR></TABLE></P><P>This declares a variable as available for exporting outside the		environment of the script itself.</P></DD><DT>-x var=$value</DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;declare -x var3=373</PRE></TD></TR></TABLE></P><P>The <BCLASS="COMMAND">declare</B> command permits		  assigning a value to a variable in the same statement		  as setting its properties.</P></DD></DL></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX20"></A><P><B>Example 9-23. Using <ICLASS="FIRSTTERM">declare</I> to type variables</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;func1 ()   4&nbsp;{   5&nbsp;echo This is a function.   6&nbsp;}   7&nbsp;   8&nbsp;declare -f        # Lists the function above.   9&nbsp;  10&nbsp;echo  11&nbsp;  12&nbsp;declare -i var1   # var1 is an integer.  13&nbsp;var1=2367  14&nbsp;echo "var1 declared as $var1"  15&nbsp;var1=var1+1       # Integer declaration eliminates the need for 'let'.  16&nbsp;echo "var1 incremented by 1 is $var1."  17&nbsp;# Attempt to change variable declared as integer.  18&nbsp;echo "Attempting to change var1 to floating point value, 2367.1."  19&nbsp;var1=2367.1       # Results in error message, with no change to variable.  20&nbsp;echo "var1 is still $var1"  21&nbsp;  22&nbsp;echo  23&nbsp;  24&nbsp;declare -r var2=13.36         # 'declare' permits setting a variable property  25&nbsp;                              #+ and simultaneously assigning it a value.  26&nbsp;echo "var2 declared as $var2" # Attempt to change readonly variable.  27&nbsp;var2=13.37                    # Generates error message, and exit from script.  28&nbsp;  29&nbsp;echo "var2 is still $var2"    # This line will not execute.  30&nbsp;  31&nbsp;exit 0                        # Script will not exit here.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Using the <ICLASS="FIRSTTERM">declare</I> builtin	  restricts the <AHREF="subshells.html#SCOPEREF">scope</A>	  of a variable.	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;foo ()   2&nbsp;{   3&nbsp;FOO="bar"   4&nbsp;}   5&nbsp;   6&nbsp;bar ()   7&nbsp;{   8&nbsp;foo   9&nbsp;echo $FOO  10&nbsp;}  11&nbsp;  12&nbsp;bar   # Prints bar.</PRE></TD></TR></TABLE></P><P>However . . .	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;foo (){   2&nbsp;declare FOO="bar"   3&nbsp;}   4&nbsp;   5&nbsp;bar ()   6&nbsp;{   7&nbsp;foo   8&nbsp;echo $FOO   9&nbsp;}  10&nbsp;  11&nbsp;bar  # Prints nothing.  12&nbsp;  13&nbsp;  14&nbsp;# Thank you, Michael Iatrou, for pointing this out.</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN5568"HREF="declareref.html#AEN5568">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="TYPINGREF"></A>In this context,	    <ICLASS="FIRSTTERM">typing</I>	    a variable means to classify it and restrict its properties.	    For example, a variable <ICLASS="FIRSTTERM">declared</I>	    or <ICLASS="FIRSTTERM">typed</I> as an integer	    is no longer available for <AHREF="refcards.html#STRINGOPSTAB">string	    operations</A>.</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="parameter-substitution.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="ivr.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Parameter Substitution</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Indirect References</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -