Debian Codegraph @

HTML/numerical-constants.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Numerical Constants</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="Operations and Related Topics"
18 HREF="operations.html"><LINK
19 REL="NEXT"
20 TITLE="The Double-Parentheses Construct"
21 HREF="dblparens.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="operations.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="dblparens.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="NUMERICAL-CONSTANTS"
96 ></A
97 >8.2. Numerical Constants</H1
98 ><P
99 ><A
100 NAME="NUMCONSTANTS"
101 ></A
102 >A shell script interprets a number
103 as decimal (base 10), unless that number has a
104 special prefix or notation. A number preceded by a
105 <TT
106 CLASS="REPLACEABLE"
107 ><I
108 >0</I
109 ></TT
110 > is <TT
111 CLASS="REPLACEABLE"
112 ><I
113 >octal</I
114 ></TT
115 >
116 (base 8). A number preceded by <TT
117 CLASS="REPLACEABLE"
118 ><I
119 >0x</I
120 ></TT
121 >
122 is <TT
123 CLASS="REPLACEABLE"
124 ><I
125 >hexadecimal</I
126 ></TT
127 > (base 16). A number
128 with an embedded <TT
129 CLASS="REPLACEABLE"
130 ><I
131 >#</I
132 ></TT
133 > evaluates as
134 <TT
135 CLASS="REPLACEABLE"
136 ><I
137 >BASE#NUMBER</I
138 ></TT
139 > (with range and notational
140 restrictions).</P
141 ><DIV
142 CLASS="EXAMPLE"
143 ><HR><A
144 NAME="NUMBERS"
145 ></A
146 ><P
147 ><B
148 >Example 8-4. Representation of numerical constants</B
149 ></P
150 ><TABLE
151 BORDER="0"
152 BGCOLOR="#E0E0E0"
153 WIDTH="100%"
154 ><TR
155 ><TD
156 ><PRE
157 CLASS="PROGRAMLISTING"
158 > 1&nbsp;#!/bin/bash
159 2&nbsp;# numbers.sh: Representation of numbers in different bases.
160 3&nbsp;
161 4&nbsp;# Decimal: the default
162 5&nbsp;let "dec = 32"
163 6&nbsp;echo "decimal number = $dec" # 32
164 7&nbsp;# Nothing out of the ordinary here.
165 8&nbsp;
166 9&nbsp;
167 10&nbsp;# Octal: numbers preceded by '0' (zero)
168 11&nbsp;let "oct = 032"
169 12&nbsp;echo "octal number = $oct" # 26
170 13&nbsp;# Expresses result in decimal.
171 14&nbsp;# --------- ------ -- -------
172 15&nbsp;
173 16&nbsp;
174 17&nbsp;# Hexadecimal: numbers preceded by '0x' or '0X'
175 18&nbsp;let "hex = 0x32"
176 19&nbsp;echo "hexadecimal number = $hex" # 50
177 20&nbsp;
178 21&nbsp;echo $((0x9abc)) # 39612
179 22&nbsp;# ^^ ^^ double-parentheses arithmetic expansion/evaluation
180 23&nbsp;# Expresses result in decimal.
181 24&nbsp;
182 25&nbsp;
183 26&nbsp;
184 27&nbsp;# Other bases: BASE#NUMBER
185 28&nbsp;# BASE between 2 and 64.
186 29&nbsp;# NUMBER must use symbols within the BASE range, see below.
187 30&nbsp;
188 31&nbsp;
189 32&nbsp;let "bin = 2#111100111001101"
190 33&nbsp;echo "binary number = $bin" # 31181
191 34&nbsp;
192 35&nbsp;let "b32 = 32#77"
193 36&nbsp;echo "base-32 number = $b32" # 231
194 37&nbsp;
195 38&nbsp;let "b64 = 64#@_"
196 39&nbsp;echo "base-64 number = $b64" # 4031
197 40&nbsp;# This notation only works for a limited range (2 - 64) of ASCII characters.
198 41&nbsp;# 10 digits + 26 lowercase characters + 26 uppercase characters + @ + _
199 42&nbsp;
200 43&nbsp;
201 44&nbsp;echo
202 45&nbsp;
203 46&nbsp;echo $((36#zz)) $((2#10101010)) $((16#AF16)) $((53#1aA))
204 47&nbsp; # 1295 170 44822 3375
205 48&nbsp;
206 49&nbsp;
207 50&nbsp;# Important note:
208 51&nbsp;# --------------
209 52&nbsp;# Using a digit out of range of the specified base notation
210 53&nbsp;#+ gives an error message.
211 54&nbsp;
212 55&nbsp;let "bad_oct = 081"
213 56&nbsp;# (Partial) error message output:
214 57&nbsp;# bad_oct = 081: value too great for base (error token is "081")
215 58&nbsp;# Octal numbers use only digits in the range 0 - 7.
216 59&nbsp;
217 60&nbsp;exit $? # Exit value = 1 (error)
218 61&nbsp;
219 62&nbsp;# Thanks, Rich Bartell and Stephane Chazelas, for clarification.</PRE
220 ></TD
221 ></TR
222 ></TABLE
223 ><HR></DIV
224 ></DIV
225 ><DIV
226 CLASS="NAVFOOTER"
227 ><HR
228 ALIGN="LEFT"
229 WIDTH="100%"><TABLE
230 SUMMARY="Footer navigation table"
231 WIDTH="100%"
232 BORDER="0"
233 CELLPADDING="0"
234 CELLSPACING="0"
235 ><TR
236 ><TD
237 WIDTH="33%"
238 ALIGN="left"
239 VALIGN="top"
240 ><A
241 HREF="operations.html"
242 ACCESSKEY="P"
243 >Prev</A
244 ></TD
245 ><TD
246 WIDTH="34%"
247 ALIGN="center"
248 VALIGN="top"
249 ><A
250 HREF="index.html"
251 ACCESSKEY="H"
252 >Home</A
253 ></TD
254 ><TD
255 WIDTH="33%"
256 ALIGN="right"
257 VALIGN="top"
258 ><A
259 HREF="dblparens.html"
260 ACCESSKEY="N"
261 >Next</A
262 ></TD
263 ></TR
264 ><TR
265 ><TD
266 WIDTH="33%"
267 ALIGN="left"
268 VALIGN="top"
269 >Operations and Related Topics</TD
270 ><TD
271 WIDTH="34%"
272 ALIGN="center"
273 VALIGN="top"
274 ><A
275 HREF="operations.html"
276 ACCESSKEY="U"
277 >Up</A
278 ></TD
279 ><TD
280 WIDTH="33%"
281 ALIGN="right"
282 VALIGN="top"
283 >The Double-Parentheses Construct</TD
284 ></TR
285 ></TABLE
286 ></DIV
287 ></BODY
288 ></HTML
289 >