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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Local Variables</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="Functions"
HREF="functions.html"><LINK
REL="PREVIOUS"
TITLE="Functions"
HREF="functions.html"><LINK
REL="NEXT"
TITLE="Recursion Without Local Variables"
HREF="recurnolocvar.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="functions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 24. Functions</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="recurnolocvar.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="LOCALVAR"
></A
>24.2. Local Variables</H1
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="LOCALREF1"
></A
>What makes a variable
<I
CLASS="FIRSTTERM"
>local</I
>?</B
></P
><DL
><DT
>local variables</DT
><DD
><P
>A variable declared as <I
CLASS="FIRSTTERM"
>local</I
>
is one that is visible only within the <A
HREF="special-chars.html#CODEBLOCKREF"
>block of code</A
> in which it
appears. It has local <A
HREF="subshells.html#SCOPEREF"
>scope</A
>.
In a function, a <I
CLASS="FIRSTTERM"
>local variable</I
> has
meaning only within that function block.
<A
NAME="AEN18568"
HREF="#FTN.AEN18568"
>[1]</A
>
</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX62"
></A
><P
><B
>Example 24-12. Local variable visibility</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # ex62.sh: Global and local variables inside a function.
3
4 func ()
5 {
6 local loc_var=23 # Declared as local variable.
7 echo # Uses the 'local' builtin.
8 echo "\"loc_var\" in function = $loc_var"
9 global_var=999 # Not declared as local.
10 # Therefore, defaults to global.
11 echo "\"global_var\" in function = $global_var"
12 }
13
14 func
15
16 # Now, to see if local variable "loc_var" exists outside the function.
17
18 echo
19 echo "\"loc_var\" outside function = $loc_var"
20 # $loc_var outside function =
21 # No, $loc_var not visible globally.
22 echo "\"global_var\" outside function = $global_var"
23 # $global_var outside function = 999
24 # $global_var is visible globally.
25 echo
26
27 exit 0
28 # In contrast to C, a Bash variable declared inside a function
29 #+ is local ONLY if declared as such.</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
>Before a function is called, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>all</I
></SPAN
>
variables declared within the function are invisible outside
the body of the function, not just those explicitly declared
as <I
CLASS="FIRSTTERM"
>local</I
>.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 func ()
4 {
5 global_var=37 # Visible only within the function block
6 #+ before the function has been called.
7 } # END OF FUNCTION
8
9 echo "global_var = $global_var" # global_var =
10 # Function "func" has not yet been called,
11 #+ so $global_var is not visible here.
12
13 func
14 echo "global_var = $global_var" # global_var = 37
15 # Has been set by function call.</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="EXITVALANOMALY01"
></A
></P
><P
>As Evgeniy Ivanov points out, when declaring and
setting a local variable in a single command, apparently the
order of operations is to <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>first set the variable,
and only afterwards restrict it to local scope</I
></SPAN
>.
This is reflected in the <A
HREF="exit-status.html#EXITSTATUSREF"
>return value</A
>.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 echo "==OUTSIDE Function (global)=="
4 t=$(exit 1)
5 echo $? # 1
6 # As expected.
7 echo
8
9 function0 ()
10 {
11
12 echo "==INSIDE Function=="
13 echo "Global"
14 t0=$(exit 1)
15 echo $? # 1
16 # As expected.
17
18 echo
19 echo "Local declared & assigned in same command."
20 local t1=$(exit 1)
21 echo $? # 0
22 # Unexpected!
23 # Apparently, the variable assignment takes place before
24 #+ the local declaration.
25 #+ The return value is for the latter.
26
27 echo
28 echo "Local declared, then assigned (separate commands)."
29 local t2
30 t2=$(exit 1)
31 echo $? # 1
32 # As expected.
33
34 }
35
36 function0</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
></DD
></DL
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LOCVARRECUR"
></A
>24.2.1. Local variables and recursion.</H2
><P
><A
NAME="RECURSIONREF0"
></A
></P
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN18598"
></A
><P
><A
NAME="RECURSIONREF"
></A
></P
><P
><I
CLASS="FIRSTTERM"
>Recursion</I
> is an interesting
and sometimes useful form of
<I
CLASS="FIRSTTERM"
>self-reference</I
>. <A
HREF="biblio.html#MAYERREF"
>Herbert Mayer</A
> defines it
as <SPAN
CLASS="QUOTE"
>". . . expressing an algorithm by using a
simpler version of that same algorithm . . ."</SPAN
></P
><P
>Consider a definition defined in terms of itself,
<A
NAME="AEN18607"
HREF="#FTN.AEN18607"
>[2]</A
>
an expression implicit in its own expression,
<A
NAME="AEN18610"
HREF="#FTN.AEN18610"
>[3]</A
>
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>a snake swallowing its own
tail</I
></SPAN
>,
<A
NAME="AEN18614"
HREF="#FTN.AEN18614"
>[4]</A
>
or . . . a function that calls itself.
<A
NAME="AEN18617"
HREF="#FTN.AEN18617"
>[5]</A
>
</P
><P
><A
NAME="RECURSIONDEMO0"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="RECURSIONDEMO"
></A
><P
><B
>Example 24-13. Demonstration of a simple recursive function</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # recursion-demo.sh
3 # Demonstration of recursion.
4
5 RECURSIONS=9 # How many times to recurse.
6 r_count=0 # Must be global. Why?
7
8 recurse ()
9 {
10 var="$1"
11
12 while [ "$var" -ge 0 ]
13 do
14 echo "Recursion count = "$r_count" +-+ \$var = "$var""
15 (( var-- )); (( r_count++ ))
16 recurse "$var" # Function calls itself (recurses)
17 done #+ until what condition is met?
18 }
19
20 recurse $RECURSIONS
21
22 exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="RECURSIONDEMO02"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="RECURSIONDEMO2"
></A
><P
><B
>Example 24-14. Another simple demonstration</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # recursion-def.sh
3 # A script that defines "recursion" in a rather graphic way.
4
5 RECURSIONS=10
6 r_count=0
7 sp=" "
8
9 define_recursion ()
10 {
11 ((r_count++))
12 sp="$sp"" "
13 echo -n "$sp"
14 echo "\"The act of recurring ... \"" # Per 1913 Webster's dictionary.
15
16 while [ $r_count -le $RECURSIONS ]
17 do
18 define_recursion
19 done
20 }
21
22 echo
23 echo "Recursion: "
24 define_recursion
25 echo
26
27 exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
></DIV
></TD
></TR
></TABLE
><P
>Local variables are a useful tool for writing recursive
code, but this practice generally involves a great deal of
computational overhead and is definitely
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> recommended in a shell script.
<A
NAME="AEN18632"
HREF="#FTN.AEN18632"
>[6]</A
>
</P
><P
><A
NAME="FACTORIALREF"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX63"
></A
><P
><B
>Example 24-15. Recursion, using a local variable</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 # factorial
4 # ---------
5
6
7 # Does bash permit recursion?
8 # Well, yes, but...
9 # It's so slow that you gotta have rocks in your head to try it.
10
11
12 MAX_ARG=5
13 E_WRONG_ARGS=85
14 E_RANGE_ERR=86
15
16
17 if [ -z "$1" ]
18 then
19 echo "Usage: `basename $0` number"
20 exit $E_WRONG_ARGS
21 fi
22
23 if [ "$1" -gt $MAX_ARG ]
24 then
25 echo "Out of range ($MAX_ARG is maximum)."
26 # Let's get real now.
27 # If you want greater range than this,
28 #+ rewrite it in a Real Programming Language.
29 exit $E_RANGE_ERR
30 fi
31
32 fact ()
33 {
34 local number=$1
35 # Variable "number" must be declared as local,
36 #+ otherwise this doesn't work.
37 if [ "$number" -eq 0 ]
38 then
39 factorial=1 # Factorial of 0 = 1.
40 else
41 let "decrnum = number - 1"
42 fact $decrnum # Recursive function call (the function calls itself).
43 let "factorial = $number * $?"
44 fi
45
46 return $factorial
47 }
48
49 fact $1
50 echo "Factorial of $1 is $?."
51
52 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>Also see <A
HREF="contributed-scripts.html#PRIMES"
>Example A-15</A
> for an example of
recursion in a script. Be aware that recursion is
resource-intensive and executes slowly, and is therefore
generally not appropriate in a script.</P
></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.AEN18568"
HREF="localvar.html#AEN18568"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>However, as Thomas Braunberger points out, a local
variable declared in a function <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>is also visible
to functions called by the parent
function.</I
></SPAN
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 function1 ()
4 {
5 local func1var=20
6
7 echo "Within function1, \$func1var = $func1var."
8
9 function2
10 }
11
12 function2 ()
13 {
14 echo "Within function2, \$func1var = $func1var."
15 }
16
17 function1
18
19 exit 0
20
21
22 # Output of the script:
23
24 # Within function1, $func1var = 20.
25 # Within function2, $func1var = 20.</PRE
></TD
></TR
></TABLE
></P
><P
>This is documented in the Bash manual:</P
><P
><SPAN
CLASS="QUOTE"
>"Local can only be used within a function;
it makes the variable name have a visible scope
restricted to that function <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>and its
children</I
></SPAN
>."</SPAN
> [emphasis added]
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>The ABS Guide author considers this behavior
to be a bug.</I
></SPAN
></P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN18607"
HREF="localvar.html#AEN18607"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Otherwise known as
<I
CLASS="FIRSTTERM"
>redundancy</I
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN18610"
HREF="localvar.html#AEN18610"
>[3]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Otherwise known as
<I
CLASS="FIRSTTERM"
>tautology</I
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN18614"
HREF="localvar.html#AEN18614"
>[4]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Otherwise known as a
<I
CLASS="FIRSTTERM"
>metaphor</I
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN18617"
HREF="localvar.html#AEN18617"
>[5]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Otherwise known as a
<I
CLASS="FIRSTTERM"
>recursive function</I
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN18632"
HREF="localvar.html#AEN18632"
>[6]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Too many levels of recursion may crash a
script with a segfault.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 # Warning: Running this script could possibly lock up your system!
4 # If you're lucky, it will segfault before using up all available memory.
5
6 recursive_function ()
7 {
8 echo "$1" # Makes the function do something, and hastens the segfault.
9 (( $1 < $2 )) && recursive_function $(( $1 + 1 )) $2;
10 # As long as 1st parameter is less than 2nd,
11 #+ increment 1st and recurse.
12 }
13
14 recursive_function 1 50000 # Recurse 50,000 levels!
15 # Most likely segfaults (depending on stack size, set by ulimit -m).
16
17 # Recursion this deep might cause even a C program to segfault,
18 #+ by using up all the memory allotted to the stack.
19
20
21 echo "This will probably not print."
22 exit 0 # This script will not exit normally.
23
24 # Thanks, Stphane Chazelas.</PRE
></TD
></TR
></TABLE
></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="functions.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="recurnolocvar.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Functions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="functions.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Recursion Without Local Variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|