Debian Codegraph @

HTML/untyped.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Bash Variables Are Untyped</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="Introduction to Variables and Parameters"
15 HREF="variables.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Variable Assignment"
18 HREF="varassignment.html"><LINK
19 REL="NEXT"
20 TITLE="Special Variable Types"
21 HREF="othertypesv.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="varassignment.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 4. Introduction to Variables and Parameters</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="othertypesv.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="UNTYPED"
96 ></A
97 >4.3. Bash Variables Are Untyped</H1
98 ><P
99 ><A
100 NAME="BVUNTYPED"
101 ></A
102 ></P
103 ><P
104 >Unlike many other programming languages, Bash does not segregate
105 its variables by <SPAN
106 CLASS="QUOTE"
107 >"type."</SPAN
108 > Essentially, <SPAN
109 CLASS="emphasis"
110 ><I
111 CLASS="EMPHASIS"
112 >Bash
113 variables are character strings</I
114 ></SPAN
115 >, but, depending on
116 context, Bash permits arithmetic operations and comparisons on
117 variables. The determining factor is whether the value of a
118 variable contains only digits.</P
119 ><DIV
120 CLASS="EXAMPLE"
121 ><HR><A
122 NAME="INTORSTRING"
123 ></A
124 ><P
125 ><B
126 >Example 4-4. Integer or string?</B
127 ></P
128 ><TABLE
129 BORDER="0"
130 BGCOLOR="#E0E0E0"
131 WIDTH="100%"
132 ><TR
133 ><TD
134 ><PRE
135 CLASS="PROGRAMLISTING"
136 > 1&nbsp;#!/bin/bash
137 2&nbsp;# int-or-string.sh
138 3&nbsp;
139 4&nbsp;a=2334 # Integer.
140 5&nbsp;let "a += 1"
141 6&nbsp;echo "a = $a " # a = 2335
142 7&nbsp;echo # Integer, still.
143 8&nbsp;
144 9&nbsp;
145 10&nbsp;b=${a/23/BB} # Substitute "BB" for "23".
146 11&nbsp; # This transforms $b into a string.
147 12&nbsp;echo "b = $b" # b = BB35
148 13&nbsp;declare -i b # Declaring it an integer doesn't help.
149 14&nbsp;echo "b = $b" # b = BB35
150 15&nbsp;
151 16&nbsp;let "b += 1" # BB35 + 1
152 17&nbsp;echo "b = $b" # b = 1
153 18&nbsp;echo # Bash sets the "integer value" of a string to 0.
154 19&nbsp;
155 20&nbsp;c=BB34
156 21&nbsp;echo "c = $c" # c = BB34
157 22&nbsp;d=${c/BB/23} # Substitute "23" for "BB".
158 23&nbsp; # This makes $d an integer.
159 24&nbsp;echo "d = $d" # d = 2334
160 25&nbsp;let "d += 1" # 2334 + 1
161 26&nbsp;echo "d = $d" # d = 2335
162 27&nbsp;echo
163 28&nbsp;
164 29&nbsp;
165 30&nbsp;# What about null variables?
166 31&nbsp;e='' # ... Or e="" ... Or e=
167 32&nbsp;echo "e = $e" # e =
168 33&nbsp;let "e += 1" # Arithmetic operations allowed on a null variable?
169 34&nbsp;echo "e = $e" # e = 1
170 35&nbsp;echo # Null variable transformed into an integer.
171 36&nbsp;
172 37&nbsp;# What about undeclared variables?
173 38&nbsp;echo "f = $f" # f =
174 39&nbsp;let "f += 1" # Arithmetic operations allowed?
175 40&nbsp;echo "f = $f" # f = 1
176 41&nbsp;echo # Undeclared variable transformed into an integer.
177 42&nbsp;#
178 43&nbsp;# However ...
179 44&nbsp;let "f /= $undecl_var" # Divide by zero?
180 45&nbsp;# let: f /= : syntax error: operand expected (error token is " ")
181 46&nbsp;# Syntax error! Variable $undecl_var is not set to zero here!
182 47&nbsp;#
183 48&nbsp;# But still ...
184 49&nbsp;let "f /= 0"
185 50&nbsp;# let: f /= 0: division by 0 (error token is "0")
186 51&nbsp;# Expected behavior.
187 52&nbsp;
188 53&nbsp;
189 54&nbsp;# Bash (usually) sets the "integer value" of null to zero
190 55&nbsp;#+ when performing an arithmetic operation.
191 56&nbsp;# But, don't try this at home, folks!
192 57&nbsp;# It's undocumented and probably non-portable behavior.
193 58&nbsp;
194 59&nbsp;
195 60&nbsp;# Conclusion: Variables in Bash are untyped,
196 61&nbsp;#+ with all attendant consequences.
197 62&nbsp;
198 63&nbsp;exit $?</PRE
199 ></TD
200 ></TR
201 ></TABLE
202 ><HR></DIV
203 ><P
204 >Untyped variables are both a blessing and a curse. They permit
205 more flexibility in scripting and make it easier to grind out
206 lines of code (and give you enough rope to hang yourself!).
207 However, they likewise permit subtle errors to creep in
208 and encourage sloppy programming habits.</P
209 ><P
210 >To lighten the burden of keeping track of variable
211 types in a script, Bash <SPAN
212 CLASS="emphasis"
213 ><I
214 CLASS="EMPHASIS"
215 >does</I
216 ></SPAN
217 > permit
218 <A
219 HREF="declareref.html"
220 >declaring</A
221 > variables.</P
222 ></DIV
223 ><DIV
224 CLASS="NAVFOOTER"
225 ><HR
226 ALIGN="LEFT"
227 WIDTH="100%"><TABLE
228 SUMMARY="Footer navigation table"
229 WIDTH="100%"
230 BORDER="0"
231 CELLPADDING="0"
232 CELLSPACING="0"
233 ><TR
234 ><TD
235 WIDTH="33%"
236 ALIGN="left"
237 VALIGN="top"
238 ><A
239 HREF="varassignment.html"
240 ACCESSKEY="P"
241 >Prev</A
242 ></TD
243 ><TD
244 WIDTH="34%"
245 ALIGN="center"
246 VALIGN="top"
247 ><A
248 HREF="index.html"
249 ACCESSKEY="H"
250 >Home</A
251 ></TD
252 ><TD
253 WIDTH="33%"
254 ALIGN="right"
255 VALIGN="top"
256 ><A
257 HREF="othertypesv.html"
258 ACCESSKEY="N"
259 >Next</A
260 ></TD
261 ></TR
262 ><TR
263 ><TD
264 WIDTH="33%"
265 ALIGN="left"
266 VALIGN="top"
267 >Variable Assignment</TD
268 ><TD
269 WIDTH="34%"
270 ALIGN="center"
271 VALIGN="top"
272 ><A
273 HREF="variables.html"
274 ACCESSKEY="U"
275 >Up</A
276 ></TD
277 ><TD
278 WIDTH="33%"
279 ALIGN="right"
280 VALIGN="top"
281 >Special Variable Types</TD
282 ></TR
283 ></TABLE
284 ></DIV
285 ></BODY
286 ></HTML
287 >