📄 arithexp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Arithmetic Expansion</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Beyond the Basics"HREF="part3.html"><LINKREL="PREVIOUS"TITLE="Command Substitution"HREF="commandsub.html"><LINKREL="NEXT"TITLE="Recess Time"HREF="recess-time.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="commandsub.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="recess-time.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="ARITHEXP"></A>Chapter 12. Arithmetic Expansion</H1><P><ANAME="ARITHEXPREF"></A>Arithmetic expansion provides a powerful tool for performing (integer) arithmetic operations in scripts. Translating a string into a numerical expression is relatively straightforward using <ICLASS="FIRSTTERM">backticks</I>, <ICLASS="FIRSTTERM">double parentheses</I>, or <ICLASS="FIRSTTERM">let</I>.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="ARITHEXPVAR1"></A>Variations</B></P><DL><DT>Arithmetic expansion with <AHREF="commandsub.html#BACKQUOTESREF">backticks</A> (often used in conjunction with <AHREF="moreadv.html#EXPRREF">expr</A>)</DT><DD><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 z=`expr $z + 3` # The 'expr' command performs the expansion.</PRE></TD></TR></TABLE></P></DD><DT>Arithmetic expansion with <AHREF="dblparens.html">double parentheses</A>, and using <AHREF="internal.html#LETREF">let</A></DT><DD><P>The use of <ICLASS="FIRSTTERM">backticks</I> (<ICLASS="FIRSTTERM">backquotes</I>) in arithmetic expansion has been superseded by <ICLASS="FIRSTTERM">double parentheses</I> -- <TTCLASS="USERINPUT"><B>((...))</B></TT> and <TTCLASS="USERINPUT"><B>$((...))</B></TT> -- and also by the very convenient <BCLASS="COMMAND">let</B> construction.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 z=$(($z+3)) 2 z=$((z+3)) # Also correct. 3 # Within double parentheses, 4 #+ parameter dereferencing 5 #+ is optional. 6 7 # $((EXPRESSION)) is arithmetic expansion. # Not to be confused with 8 #+ command substitution. 9 10 11 12 # You may also use operations within double parentheses without assignment. 13 14 n=0 15 echo "n = $n" # n = 0 16 17 (( n += 1 )) # Increment. 18 # (( $n += 1 )) is incorrect! 19 echo "n = $n" # n = 1 20 21 22 let z=z+3 23 let "z += 3" # Quotes permit the use of spaces in variable assignment. 24 # The 'let' operator actually performs arithmetic evaluation, 25 #+ rather than expansion.</PRE></TD></TR></TABLE> </P><P>Examples of arithmetic expansion in scripts: <OLTYPE="1"><LI><P><AHREF="moreadv.html#EX45">Example 15-9</A></P></LI><LI><P><AHREF="loops.html#EX25">Example 10-14</A></P></LI><LI><P><AHREF="arrays.html#EX66">Example 26-1</A></P></LI><LI><P><AHREF="arrays.html#BUBBLE">Example 26-11</A></P></LI><LI><P><AHREF="contributed-scripts.html#TREE">Example A-17</A></P></LI></OL> </P></DD></DL></DIV></DIV><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="commandsub.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="recess-time.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Command Substitution</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part3.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Recess Time</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -