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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Bash, version 3</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="Bash, versions 2, 3, and 4"
HREF="bash2.html"><LINK
REL="PREVIOUS"
TITLE="Bash, versions 2, 3, and 4"
HREF="bash2.html"><LINK
REL="NEXT"
TITLE="Bash, version 4"
HREF="bashver4.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="bash2.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 37. Bash, versions 2, 3, and 4</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="bashver4.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="BASHVER3"
></A
>37.2. Bash, version 3</H1
><P
><A
NAME="BASH3REF"
></A
></P
><P
>On July 27, 2004, Chet Ramey released version 3 of Bash.
This update fixed quite a number of bugs and added new
features.</P
><P
>Some of the more important added features:
<UL
><LI
><P
><A
NAME="BRACEEXPREF3"
></A
></P
><P
>A new, more generalized <B
CLASS="COMMAND"
>{a..z}</B
> <A
HREF="special-chars.html#BRACEEXPREF"
>brace expansion</A
> operator.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 for i in {1..10}
4 # Simpler and more straightforward than
5 #+ for i in $(seq 10)
6 do
7 echo -n "$i "
8 done
9
10 echo
11
12 # 1 2 3 4 5 6 7 8 9 10
13
14
15
16 # Or just . . .
17
18 echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
19 echo {e..m} # e f g h i j k l m
20 echo {z..a} # z y x w v u t s r q p o n m l k j i h g f e d c b a
21 # Works backwards, too.
22 echo {25..30} # 25 26 27 28 29 30
23 echo {3..-2} # 3 2 1 0 -1 -2
24 echo {X..d} # X Y Z [ ] ^ _ ` a b c d
25 # Shows (some of) the ASCII characters between Z and a,
26 #+ but don't rely on this type of behavior because . . .
27 echo {]..a} # {]..a}
28 # Why?
29
30
31 # You can tack on prefixes and suffixes.
32 echo "Number #"{1..4}, "..."
33 # Number #1, Number #2, Number #3, Number #4, ...
34
35
36 # You can concatenate brace-expansion sets.
37 echo {1..3}{x..z}" +" "..."
38 # 1x + 1y + 1z + 2x + 2y + 2z + 3x + 3y + 3z + ...
39 # Generates an algebraic expression.
40 # This could be used to find permutations.
41
42 # You can nest brace-expansion sets.
43 echo {{a..c},{1..3}}
44 # a b c 1 2 3
45 # The "comma operator" splices together strings.
46
47 # ########## ######### ############ ########### ######### ###############
48 # Unfortunately, brace expansion does not lend itself to parameterization.
49 var1=1
50 var2=5
51 echo {$var1..$var2} # {1..5}
52
53
54 # Yet, as Emiliano G. points out, using "eval" overcomes this limitation.
55
56 start=0
57 end=10
58 for index in $(eval echo {$start..$end})
59 do
60 echo -n "$index " # 0 1 2 3 4 5 6 7 8 9 10
61 done
62
63 echo</PRE
></TD
></TR
></TABLE
></P
></LI
><LI
><P
>The <B
CLASS="COMMAND"
>${!array[@]}</B
> operator, which
expands to all the indices of a given <A
HREF="arrays.html#ARRAYREF"
>array</A
>.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 Array=(element-zero element-one element-two element-three)
4
5 echo ${Array[0]} # element-zero
6 # First element of array.
7
8 echo ${!Array[@]} # 0 1 2 3
9 # All the indices of Array.
10
11 for i in ${!Array[@]}
12 do
13 echo ${Array[i]} # element-zero
14 # element-one
15 # element-two
16 # element-three
17 #
18 # All the elements in Array.
19 done</PRE
></TD
></TR
></TABLE
></P
></LI
><LI
><P
><A
NAME="REGEXMATCHREF"
></A
></P
><P
>The <B
CLASS="COMMAND"
>=~</B
> <A
HREF="regexp.html#REGEXREF"
>Regular
Expression</A
> matching operator within a <A
HREF="tests.html#DBLBRACKETS"
>double brackets</A
> test expression.
(Perl has a similar operator.)</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 variable="This is a fine mess."
4
5 echo "$variable"
6
7 # Regex matching with =~ operator within [[ double brackets ]].
8 if [[ "$variable" =~ T.........fin*es* ]]
9 # NOTE: As of version 3.2 of Bash, expression to match no longer quoted.
10 then
11 echo "match found"
12 # match found
13 fi</PRE
></TD
></TR
></TABLE
></P
><P
>Or, more usefully:</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 input=$1
4
5
6 if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
7 # ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.
8 # NNN-NN-NNNN (where each N is a digit).
9 then
10 echo "Social Security number."
11 # Process SSN.
12 else
13 echo "Not a Social Security number!"
14 # Or, ask for corrected input.
15 fi</PRE
></TD
></TR
></TABLE
></P
><P
>For additional examples of using the
<B
CLASS="COMMAND"
>=~</B
> operator, see <A
HREF="contributed-scripts.html#WHX"
>Example A-29</A
>,
<A
HREF="here-docs.html#MAILBOXGREP"
>Example 19-14</A
>, <A
HREF="contributed-scripts.html#FINDSPLIT"
>Example A-35</A
>, and <A
HREF="contributed-scripts.html#TOHTML"
>Example A-24</A
>.</P
></LI
><LI
><P
><A
NAME="PIPEFAILREF"
></A
></P
><P
>The new <TT
CLASS="OPTION"
>set -o pipefail</TT
> option is
useful for debugging <A
HREF="special-chars.html#PIPEREF"
>pipes</A
>. If
this option is set, then the <A
HREF="exit-status.html#EXITSTATUSREF"
>exit status</A
> of a pipe
is the exit status of the last command in the pipe to
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>fail</I
></SPAN
> (return a non-zero value), rather
than the actual final command in the pipe.</P
><P
>See <A
HREF="communications.html#FC4UPD"
>Example 16-43</A
>.</P
></LI
></UL
>
</P
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="100%"
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
>The update to version 3 of Bash breaks a few scripts
that worked under earlier versions. <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Test critical legacy
scripts to make sure they still work!</I
></SPAN
></P
><P
>As it happens, a couple of the scripts in the
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>Advanced Bash Scripting Guide</I
></SPAN
> had to be
fixed up (see <A
HREF="variables2.html#TOUT"
>Example 9-4</A
>, for instance).</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN20956"
></A
>37.2.1. Bash, version 3.1</H2
><P
>The version 3.1 update of Bash introduces a number of bugfixes
and a few minor changes.</P
><UL
><LI
><P
>The <SPAN
CLASS="TOKEN"
>+=</SPAN
> operator is now permitted in
in places where previously only the <SPAN
CLASS="TOKEN"
>=</SPAN
>
assignment operator was recognized.</P
><P
><A
NAME="PLUSEQSTR"
></A
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 a=1
2 echo $a # 1
3
4 a+=5 # Won't work under versions of Bash earlier than 3.1.
5 echo $a # 15
6
7 a+=Hello
8 echo $a # 15Hello</PRE
></TD
></TR
></TABLE
>
</P
><P
>Here, <SPAN
CLASS="TOKEN"
>+=</SPAN
> functions as a <I
CLASS="FIRSTTERM"
>string
concatenation</I
> operator. Note that its behavior
in this particular context is different than within a
<A
HREF="internal.html#LETREF"
>let</A
> construct.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 a=1
2 echo $a # 1
3
4 let a+=5 # Integer arithmetic, rather than string concatenation.
5 echo $a # 6
6
7 let a+=Hello # Doesn't "add" anything to a.
8 echo $a # 6</PRE
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="PATHAPPEND"
></A
>Jeffrey Haemer points out
that this concatenation operator can be quite
useful. In this instance, we append a directory to the
<TT
CLASS="VARNAME"
>$PATH</TT
>.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo $PATH</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>PATH+=:/opt/bin</B
></TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo $PATH</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games:/opt/bin</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
></LI
></UL
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN20987"
></A
>37.2.2. Bash, version 3.2</H2
><P
>This is pretty much a bugfix update.</P
><UL
><LI
><P
>In <A
HREF="parameter-substitution.html#PSGLOB"
><I
CLASS="FIRSTTERM"
>global</I
>
parameter substitutions</A
>, the pattern no longer anchors
at the start of the string.</P
></LI
><LI
><P
>The <TT
CLASS="OPTION"
>--wordexp</TT
> option disables
<A
HREF="process-sub.html#PROCESSSUBREF"
>process substitution</A
>.</P
></LI
><LI
><P
>The <B
CLASS="COMMAND"
>=~</B
> <A
HREF="bashver3.html#REGEXMATCHREF"
>Regular Expression
match operator</A
> no longer requires
<A
HREF="quoting.html#QUOTINGREF"
>quoting</A
> of the
<I
CLASS="FIRSTTERM"
>pattern</I
> within <A
HREF="tests.html#DBLBRACKETS"
>[[ ... ]]</A
>.</P
><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
>In fact, quoting in this context is
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> advisable as it may
cause <I
CLASS="FIRSTTERM"
>regex</I
> evaluation to fail.
Chet Ramey states in the <A
HREF="biblio.html#BASHFAQ"
>Bash
FAQ</A
> that quoting explicitly disables regex evaluation.
See also the <A
HREF="https://bugs.launchpad.net/ubuntu-website/+bug/109931"
TARGET="_top"
> Ubuntu Bug List</A
> and <A
HREF="http://en.wikinerds.org/index.php/Bash_syntax_and_semantics"
TARGET="_top"
> Wikinerds on Bash syntax</A
>.</P
><P
>Setting <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>shopt -s compat31</I
></SPAN
>
in a script causes reversion to the original
behavior.</P
></TD
></TR
></TABLE
></DIV
></LI
></UL
></DIV
></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="bash2.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="bashver4.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Bash, versions 2, 3, and 4</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="bash2.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Bash, version 4</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|