Debian Codegraph @

HTML/dblparens.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >The Double-Parentheses Construct</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="Operations and Related Topics"
15 HREF="operations.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Numerical Constants"
18 HREF="numerical-constants.html"><LINK
19 REL="NEXT"
20 TITLE="Operator Precedence"
21 HREF="opprecedence.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="SECT1"
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="numerical-constants.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 8. Operations and Related Topics</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="opprecedence.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="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="DBLPARENS"
96 ></A
97 >8.3. The Double-Parentheses Construct</H1
98 ><P
99 ><A
100 NAME="DBLPARENSREF"
101 ></A
102 ></P
103 ><P
104 >Similar to the <A
105 HREF="internal.html#LETREF"
106 >let</A
107 > command,
108 the <B
109 CLASS="COMMAND"
110 >(( ... ))</B
111 > construct permits
112 arithmetic expansion and evaluation. In its simplest
113 form, <TT
114 CLASS="USERINPUT"
115 ><B
116 >a=$(( 5 + 3 ))</B
117 ></TT
118 > would set
119 <TT
120 CLASS="USERINPUT"
121 ><B
122 >a</B
123 ></TT
124 > to <TT
125 CLASS="USERINPUT"
126 ><B
127 >5 + 3</B
128 ></TT
129 >, or
130 <TT
131 CLASS="USERINPUT"
132 ><B
133 >8</B
134 ></TT
135 >. However, this double-parentheses
136 construct is also a mechanism for allowing C-style
137 manipulation of variables in Bash, for example,
138 <TT
139 CLASS="VARNAME"
140 >(( var++ ))</TT
141 >.</P
142 ><P
143 ><A
144 NAME="PLUSPLUSREF"
145 ></A
146 ></P
147 ><DIV
148 CLASS="EXAMPLE"
149 ><HR><A
150 NAME="CVARS"
151 ></A
152 ><P
153 ><B
154 >Example 8-5. C-style manipulation of variables</B
155 ></P
156 ><TABLE
157 BORDER="0"
158 BGCOLOR="#E0E0E0"
159 WIDTH="100%"
160 ><TR
161 ><TD
162 ><PRE
163 CLASS="PROGRAMLISTING"
164 > 1&nbsp;#!/bin/bash
165 2&nbsp;# c-vars.sh
166 3&nbsp;# Manipulating a variable, C-style, using the (( ... )) construct.
167 4&nbsp;
168 5&nbsp;
169 6&nbsp;echo
170 7&nbsp;
171 8&nbsp;(( a = 23 )) # Setting a value, C-style,
172 9&nbsp; #+ with spaces on both sides of the "=".
173 10&nbsp;echo "a (initial value) = $a" # 23
174 11&nbsp;
175 12&nbsp;(( a++ )) # Post-increment 'a', C-style.
176 13&nbsp;echo "a (after a++) = $a" # 24
177 14&nbsp;
178 15&nbsp;(( a-- )) # Post-decrement 'a', C-style.
179 16&nbsp;echo "a (after a--) = $a" # 23
180 17&nbsp;
181 18&nbsp;
182 19&nbsp;(( ++a )) # Pre-increment 'a', C-style.
183 20&nbsp;echo "a (after ++a) = $a" # 24
184 21&nbsp;
185 22&nbsp;(( --a )) # Pre-decrement 'a', C-style.
186 23&nbsp;echo "a (after --a) = $a" # 23
187 24&nbsp;
188 25&nbsp;echo
189 26&nbsp;
190 27&nbsp;########################################################
191 28&nbsp;# Note that, as in C, pre- and post-decrement operators
192 29&nbsp;#+ have different side-effects.
193 30&nbsp;
194 31&nbsp;n=1; let --n &#38;&#38; echo "True" || echo "False" # False
195 32&nbsp;n=1; let n-- &#38;&#38; echo "True" || echo "False" # True
196 33&nbsp;
197 34&nbsp;# Thanks, Jeroen Domburg.
198 35&nbsp;########################################################
199 36&nbsp;
200 37&nbsp;echo
201 38&nbsp;
202 39&nbsp;(( t = a&#60;45?7:11 )) # C-style trinary operator.
203 40&nbsp;# ^ ^ ^
204 41&nbsp;echo "If a &#60; 45, then t = 7, else t = 11." # a = 23
205 42&nbsp;echo "t = $t " # t = 7
206 43&nbsp;
207 44&nbsp;echo
208 45&nbsp;
209 46&nbsp;
210 47&nbsp;# -----------------
211 48&nbsp;# Easter Egg alert!
212 49&nbsp;# -----------------
213 50&nbsp;# Chet Ramey seems to have snuck a bunch of undocumented C-style
214 51&nbsp;#+ constructs into Bash (actually adapted from ksh, pretty much).
215 52&nbsp;# In the Bash docs, Ramey calls (( ... )) shell arithmetic,
216 53&nbsp;#+ but it goes far beyond that.
217 54&nbsp;# Sorry, Chet, the secret is out.
218 55&nbsp;
219 56&nbsp;# See also "for" and "while" loops using the (( ... )) construct.
220 57&nbsp;
221 58&nbsp;# These work only with version 2.04 or later of Bash.
222 59&nbsp;
223 60&nbsp;exit</PRE
224 ></TD
225 ></TR
226 ></TABLE
227 ><HR></DIV
228 ><P
229 >See also <A
230 HREF="loops.html#FORLOOPC"
231 >Example 11-13</A
232 > and <A
233 HREF="numerical-constants.html#NUMBERS"
234 >Example 8-4</A
235 >.</P
236 ></DIV
237 ><DIV
238 CLASS="NAVFOOTER"
239 ><HR
240 ALIGN="LEFT"
241 WIDTH="100%"><TABLE
242 SUMMARY="Footer navigation table"
243 WIDTH="100%"
244 BORDER="0"
245 CELLPADDING="0"
246 CELLSPACING="0"
247 ><TR
248 ><TD
249 WIDTH="33%"
250 ALIGN="left"
251 VALIGN="top"
252 ><A
253 HREF="numerical-constants.html"
254 ACCESSKEY="P"
255 >Prev</A
256 ></TD
257 ><TD
258 WIDTH="34%"
259 ALIGN="center"
260 VALIGN="top"
261 ><A
262 HREF="index.html"
263 ACCESSKEY="H"
264 >Home</A
265 ></TD
266 ><TD
267 WIDTH="33%"
268 ALIGN="right"
269 VALIGN="top"
270 ><A
271 HREF="opprecedence.html"
272 ACCESSKEY="N"
273 >Next</A
274 ></TD
275 ></TR
276 ><TR
277 ><TD
278 WIDTH="33%"
279 ALIGN="left"
280 VALIGN="top"
281 >Numerical Constants</TD
282 ><TD
283 WIDTH="34%"
284 ALIGN="center"
285 VALIGN="top"
286 ><A
287 HREF="operations.html"
288 ACCESSKEY="U"
289 >Up</A
290 ></TD
291 ><TD
292 WIDTH="33%"
293 ALIGN="right"
294 VALIGN="top"
295 >Operator Precedence</TD
296 ></TR
297 ></TABLE
298 ></DIV
299 ></BODY
300 ></HTML
301 >