Debian Codegraph @

HTML/variables.html

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