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