Debian Codegraph @

HTML/arithexp.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Arithmetic Expansion</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Beyond the Basics"
15 HREF="part3.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Command Substitution"
18 HREF="commandsub.html"><LINK
19 REL="NEXT"
20 TITLE="Recess Time"
21 HREF="recess-time.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="CHAPTER"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="commandsub.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="recess-time.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="CHAPTER"
92 ><H1
93 ><A
94 NAME="ARITHEXP"
95 ></A
96 >Chapter 13. Arithmetic Expansion</H1
97 ><P
98 ><A
99 NAME="ARITHEXPREF"
100 ></A
101 >Arithmetic expansion provides a
102 powerful tool for performing (integer) arithmetic
103 operations in scripts. Translating a string into a
104 numerical expression is relatively straightforward using
105 <I
106 CLASS="FIRSTTERM"
107 >backticks</I
108 >, <I
109 CLASS="FIRSTTERM"
110 >double
111 parentheses</I
112 >, or <I
113 CLASS="FIRSTTERM"
114 >let</I
115 >.</P
116 ><DIV
117 CLASS="VARIABLELIST"
118 ><P
119 ><B
120 ><A
121 NAME="ARITHEXPVAR1"
122 ></A
123 >Variations</B
124 ></P
125 ><DL
126 ><DT
127 >Arithmetic expansion with <A
128 HREF="commandsub.html#BACKQUOTESREF"
129 >backticks</A
130 > (often used in
131 conjunction with <A
132 HREF="moreadv.html#EXPRREF"
133 >expr</A
134 >)</DT
135 ><DD
136 ><P
137 ><TABLE
138 BORDER="0"
139 BGCOLOR="#E0E0E0"
140 WIDTH="90%"
141 ><TR
142 ><TD
143 ><PRE
144 CLASS="PROGRAMLISTING"
145 > 1&nbsp;z=`expr $z + 3` # The 'expr' command performs the expansion.</PRE
146 ></TD
147 ></TR
148 ></TABLE
149 ></P
150 ></DD
151 ><DT
152 >Arithmetic expansion with <A
153 HREF="dblparens.html"
154 >double
155 parentheses</A
156 >, and using <A
157 HREF="internal.html#LETREF"
158 >let</A
159 ></DT
160 ><DD
161 ><P
162 >The use of <I
163 CLASS="FIRSTTERM"
164 >backticks</I
165 >
166 (<I
167 CLASS="FIRSTTERM"
168 >backquotes</I
169 >) in arithmetic
170 expansion has been superseded by <I
171 CLASS="FIRSTTERM"
172 >double
173 parentheses</I
174 > --
175 <TT
176 CLASS="USERINPUT"
177 ><B
178 >((...))</B
179 ></TT
180 > and
181 <TT
182 CLASS="USERINPUT"
183 ><B
184 >$((...))</B
185 ></TT
186 > -- and also by the very
187 convenient <A
188 HREF="internal.html#LETREF"
189 >let</A
190 > construction.</P
191 ><P
192 > <TABLE
193 BORDER="0"
194 BGCOLOR="#E0E0E0"
195 WIDTH="90%"
196 ><TR
197 ><TD
198 ><PRE
199 CLASS="PROGRAMLISTING"
200 > 1&nbsp;z=$(($z+3))
201 2&nbsp;z=$((z+3)) # Also correct.
202 3&nbsp; # Within double parentheses,
203 4&nbsp; #+ parameter dereferencing
204 5&nbsp; #+ is optional.
205 6&nbsp;
206 7&nbsp;# $((EXPRESSION)) is arithmetic expansion. # Not to be confused with
207 8&nbsp; #+ command substitution.
208 9&nbsp;
209 10&nbsp;
210 11&nbsp;
211 12&nbsp;# You may also use operations within double parentheses without assignment.
212 13&nbsp;
213 14&nbsp; n=0
214 15&nbsp; echo "n = $n" # n = 0
215 16&nbsp;
216 17&nbsp; (( n += 1 )) # Increment.
217 18&nbsp;# (( $n += 1 )) is incorrect!
218 19&nbsp; echo "n = $n" # n = 1
219 20&nbsp;
220 21&nbsp;
221 22&nbsp;let z=z+3
222 23&nbsp;let "z += 3" # Quotes permit the use of spaces in variable assignment.
223 24&nbsp; # The 'let' operator actually performs arithmetic evaluation,
224 25&nbsp; #+ rather than expansion.</PRE
225 ></TD
226 ></TR
227 ></TABLE
228 >
229 </P
230 ><P
231 >Examples of arithmetic expansion in scripts:
232 <OL
233 TYPE="1"
234 ><LI
235 ><P
236 ><A
237 HREF="moreadv.html#EX45"
238 >Example 16-9</A
239 ></P
240 ></LI
241 ><LI
242 ><P
243 ><A
244 HREF="loops.html#EX25"
245 >Example 11-15</A
246 ></P
247 ></LI
248 ><LI
249 ><P
250 ><A
251 HREF="arrays.html#EX66"
252 >Example 27-1</A
253 ></P
254 ></LI
255 ><LI
256 ><P
257 ><A
258 HREF="arrays.html#BUBBLE"
259 >Example 27-11</A
260 ></P
261 ></LI
262 ><LI
263 ><P
264 ><A
265 HREF="contributed-scripts.html#TREE"
266 >Example A-16</A
267 ></P
268 ></LI
269 ></OL
270 >
271 </P
272 ></DD
273 ></DL
274 ></DIV
275 ></DIV
276 ><DIV
277 CLASS="NAVFOOTER"
278 ><HR
279 ALIGN="LEFT"
280 WIDTH="100%"><TABLE
281 SUMMARY="Footer navigation table"
282 WIDTH="100%"
283 BORDER="0"
284 CELLPADDING="0"
285 CELLSPACING="0"
286 ><TR
287 ><TD
288 WIDTH="33%"
289 ALIGN="left"
290 VALIGN="top"
291 ><A
292 HREF="commandsub.html"
293 ACCESSKEY="P"
294 >Prev</A
295 ></TD
296 ><TD
297 WIDTH="34%"
298 ALIGN="center"
299 VALIGN="top"
300 ><A
301 HREF="index.html"
302 ACCESSKEY="H"
303 >Home</A
304 ></TD
305 ><TD
306 WIDTH="33%"
307 ALIGN="right"
308 VALIGN="top"
309 ><A
310 HREF="recess-time.html"
311 ACCESSKEY="N"
312 >Next</A
313 ></TD
314 ></TR
315 ><TR
316 ><TD
317 WIDTH="33%"
318 ALIGN="left"
319 VALIGN="top"
320 >Command Substitution</TD
321 ><TD
322 WIDTH="34%"
323 ALIGN="center"
324 VALIGN="top"
325 ><A
326 HREF="part3.html"
327 ACCESSKEY="U"
328 >Up</A
329 ></TD
330 ><TD
331 WIDTH="33%"
332 ALIGN="right"
333 VALIGN="top"
334 >Recess Time</TD
335 ></TR
336 ></TABLE
337 ></DIV
338 ></BODY
339 ></HTML
340 >