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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Introduction to Variables and Parameters</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="Basics"
HREF="part2.html"><LINK
REL="PREVIOUS"
TITLE="Special Characters"
HREF="special-chars.html"><LINK
REL="NEXT"
TITLE="Variable Assignment"
HREF="varassignment.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="CHAPTER"
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="special-chars.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="varassignment.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="VARIABLES"
></A
>Chapter 4. Introduction to Variables and Parameters</H1
><P
><I
CLASS="FIRSTTERM"
>Variables</I
> are how programming and
scripting languages represent data. A variable is nothing
more than a <I
CLASS="FIRSTTERM"
>label</I
>, a name assigned to a
location or set of locations in computer memory holding an item
of data.</P
><P
>Variables appear in arithmetic operations and manipulation of
quantities, and in string parsing.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="VARSUBN"
></A
>4.1. Variable Substitution</H1
><P
>The <I
CLASS="FIRSTTERM"
>name</I
> of a variable is a placeholder
for its <I
CLASS="FIRSTTERM"
>value</I
>, the data it holds.
Referencing (retrieving) its value is called
<I
CLASS="FIRSTTERM"
>variable substitution</I
>.</P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><SPAN
CLASS="TOKEN"
>$</SPAN
></DT
><DD
><P
><A
NAME="VARNAMEVAL"
></A
></P
><P
>Let us carefully distinguish between the
<I
CLASS="FIRSTTERM"
>name</I
> of a variable
and its <I
CLASS="FIRSTTERM"
>value</I
>. If
<TT
CLASS="USERINPUT"
><B
>variable1</B
></TT
> is the name of a
variable, then <TT
CLASS="USERINPUT"
><B
>$variable1</B
></TT
>
is a reference to its <I
CLASS="FIRSTTERM"
>value</I
>,
the data item it contains.
<A
NAME="AEN2258"
HREF="#FTN.AEN2258"
>[1]</A
>
</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>variable1=23</B
></TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo variable1</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>variable1</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo $variable1</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>23</TT
></PRE
></TD
></TR
></TABLE
>
</P
><P
>The only times a variable appears <SPAN
CLASS="QUOTE"
>"naked"</SPAN
>
-- without the <SPAN
CLASS="TOKEN"
>$</SPAN
> prefix -- is when
declared or assigned, when <I
CLASS="FIRSTTERM"
>unset</I
>,
when <A
HREF="internal.html#EXPORTREF"
>exported</A
>,
in an arithmetic expression within <A
HREF="dblparens.html"
>double parentheses
(( ... ))</A
>, or in the special case of a variable
representing a <A
HREF="debugging.html#SIGNALD"
>signal</A
>
(see <A
HREF="debugging.html#EX76"
>Example 32-5</A
>). Assignment may be with an
<SPAN
CLASS="TOKEN"
>=</SPAN
> (as in <TT
CLASS="PARAMETER"
><I
>var1=27</I
></TT
>),
in a <A
HREF="internal.html#READREF"
>read</A
> statement,
and at the head of a loop (<TT
CLASS="PARAMETER"
><I
>for var2 in 1
2 3</I
></TT
>).</P
><P
><A
NAME="DBLQUO"
></A
>Enclosing a referenced value in
<I
CLASS="FIRSTTERM"
>double quotes</I
> (<SPAN
CLASS="TOKEN"
>" ... "</SPAN
>)
does not interfere with variable substitution. This is
called <I
CLASS="FIRSTTERM"
>partial quoting</I
>, sometimes
referred to as <SPAN
CLASS="QUOTE"
>"weak quoting."</SPAN
> <A
NAME="SNGLQUO"
></A
>Using single quotes (<SPAN
CLASS="TOKEN"
>' ... '</SPAN
>)
causes the variable name to be used literally, and no
substitution will take place. This is <I
CLASS="FIRSTTERM"
>full
quoting</I
>, sometimes referred to as 'strong
quoting.' See <A
HREF="quoting.html"
>Chapter 5</A
> for a
detailed discussion.</P
><P
>Note that <TT
CLASS="USERINPUT"
><B
>$variable</B
></TT
> is actually a
simplified form of
<TT
CLASS="USERINPUT"
><B
>${variable}</B
></TT
>. In contexts
where the <TT
CLASS="USERINPUT"
><B
>$variable</B
></TT
> syntax
causes an error, the longer form may work (see <A
HREF="parameter-substitution.html"
>Section 10.2</A
>, below).</P
><P
><A
NAME="VARUNSETTING"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX9"
></A
><P
><B
>Example 4-1. Variable assignment and substitution</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # ex9.sh
3
4 # Variables: assignment and substitution
5
6 a=375
7 hello=$a
8 # ^ ^
9
10 #-------------------------------------------------------------------------
11 # No space permitted on either side of = sign when initializing variables.
12 # What happens if there is a space?
13
14 # "VARIABLE =value"
15 # ^
16 #% Script tries to run "VARIABLE" command with one argument, "=value".
17
18 # "VARIABLE= value"
19 # ^
20 #% Script tries to run "value" command with
21 #+ the environmental variable "VARIABLE" set to "".
22 #-------------------------------------------------------------------------
23
24
25 echo hello # hello
26 # Not a variable reference, just the string "hello" ...
27
28 echo $hello # 375
29 # ^ This *is* a variable reference.
30 echo ${hello} # 375
31 # Likewise a variable reference, as above.
32
33 # Quoting . . .
34 echo "$hello" # 375
35 echo "${hello}" # 375
36
37 echo
38
39 hello="A B C D"
40 echo $hello # A B C D
41 echo "$hello" # A B C D
42 # As we see, echo $hello and echo "$hello" give different results.
43 # =======================================
44 # Quoting a variable preserves whitespace.
45 # =======================================
46
47 echo
48
49 echo '$hello' # $hello
50 # ^ ^
51 # Variable referencing disabled (escaped) by single quotes,
52 #+ which causes the "$" to be interpreted literally.
53
54 # Notice the effect of different types of quoting.
55
56
57 hello= # Setting it to a null value.
58 echo "\$hello (null value) = $hello" # $hello (null value) =
59 # Note that setting a variable to a null value is not the same as
60 #+ unsetting it, although the end result is the same (see below).
61
62 # --------------------------------------------------------------
63
64 # It is permissible to set multiple variables on the same line,
65 #+ if separated by white space.
66 # Caution, this may reduce legibility, and may not be portable.
67
68 var1=21 var2=22 var3=$V3
69 echo
70 echo "var1=$var1 var2=$var2 var3=$var3"
71
72 # May cause problems with legacy versions of "sh" . . .
73
74 # --------------------------------------------------------------
75
76 echo; echo
77
78 numbers="one two three"
79 # ^ ^
80 other_numbers="1 2 3"
81 # ^ ^
82 # If there is whitespace embedded within a variable,
83 #+ then quotes are necessary.
84 # other_numbers=1 2 3 # Gives an error message.
85 echo "numbers = $numbers"
86 echo "other_numbers = $other_numbers" # other_numbers = 1 2 3
87 # Escaping the whitespace also works.
88 mixed_bag=2\ ---\ Whatever
89 # ^ ^ Space after escape (\).
90
91 echo "$mixed_bag" # 2 --- Whatever
92
93 echo; echo
94
95 echo "uninitialized_variable = $uninitialized_variable"
96 # Uninitialized variable has null value (no value at all!).
97 uninitialized_variable= # Declaring, but not initializing it --
98 #+ same as setting it to a null value, as above.
99 echo "uninitialized_variable = $uninitialized_variable"
100 # It still has a null value.
101
102 uninitialized_variable=23 # Set it.
103 unset uninitialized_variable # Unset it.
104 echo "uninitialized_variable = $uninitialized_variable"
105 # uninitialized_variable =
106 # It still has a null value.
107 echo
108
109 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/caution.png"
HSPACE="5"
ALT="Caution"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="UNINITVAR1"
></A
></P
><P
>An uninitialized variable has a
<SPAN
CLASS="QUOTE"
>"null"</SPAN
> value -- no assigned value at all
(<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> zero!).
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 if [ -z "$unassigned" ]
2 then
3 echo "\$unassigned is NULL."
4 fi # $unassigned is NULL.</PRE
></TD
></TR
></TABLE
></P
><P
>Using a variable before
assigning a value to it may cause problems.
It is nevertheless possible to perform arithmetic operations
on an uninitialized variable.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 echo "$uninitialized" # (blank line)
2 let "uninitialized += 5" # Add 5 to it.
3 echo "$uninitialized" # 5
4
5 # Conclusion:
6 # An uninitialized variable has no value,
7 #+ however it evaluates as 0 in an arithmetic operation.</PRE
></TD
></TR
></TABLE
>
See also <A
HREF="internal.html#SELFSOURCE"
>Example 15-23</A
>.</P
></TD
></TR
></TABLE
></DIV
></DD
></DL
></DIV
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN2258"
HREF="variables.html#AEN2258"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><A
NAME="LVALUEREF"
></A
>Technically, the
<I
CLASS="FIRSTTERM"
>name</I
> of a variable is called an
<I
CLASS="FIRSTTERM"
>lvalue</I
>, meaning that it appears
on the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>left</I
></SPAN
> side of an assignment
statment, as in <TT
CLASS="USERINPUT"
><B
>VARIABLE=23</B
></TT
>.
A variable's <I
CLASS="FIRSTTERM"
>value</I
> is
an <I
CLASS="FIRSTTERM"
>rvalue</I
>, meaning that
it appears on the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>right</I
></SPAN
>
side of an assignment statement, as in
<TT
CLASS="USERINPUT"
><B
>VAR2=$VARIABLE</B
></TT
>.</P
><P
><A
NAME="POINTERREF"
></A
>A variable's
<I
CLASS="FIRSTTERM"
>name</I
> is, in fact,
a <I
CLASS="FIRSTTERM"
>reference</I
>, a
<I
CLASS="FIRSTTERM"
>pointer</I
> to the memory
location(s) where the actual data associated with
that variable is kept.</P
></TD
></TR
></TABLE
><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="special-chars.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="varassignment.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Special Characters</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part2.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Variable Assignment</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|