Debian Codegraph @

HTML/bashver3.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Bash, version 3</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="Bash, versions 2, 3, and 4"
15 HREF="bash2.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Bash, versions 2, 3, and 4"
18 HREF="bash2.html"><LINK
19 REL="NEXT"
20 TITLE="Bash, version 4"
21 HREF="bashver4.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="SECT1"
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="bash2.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 37. Bash, versions 2, 3, and 4</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="bashver4.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="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="BASHVER3"
96 ></A
97 >37.2. Bash, version 3</H1
98 ><P
99 ><A
100 NAME="BASH3REF"
101 ></A
102 ></P
103 ><P
104 >On July 27, 2004, Chet Ramey released version 3 of Bash.
105 This update fixed quite a number of bugs and added new
106 features.</P
107 ><P
108 >Some of the more important added features:
109
110 <UL
111 ><LI
112 ><P
113 ><A
114 NAME="BRACEEXPREF3"
115 ></A
116 ></P
117 ><P
118 >A new, more generalized <B
119 CLASS="COMMAND"
120 >{a..z}</B
121 > <A
122 HREF="special-chars.html#BRACEEXPREF"
123 >brace expansion</A
124 > operator.</P
125 ><P
126 ><TABLE
127 BORDER="0"
128 BGCOLOR="#E0E0E0"
129 WIDTH="90%"
130 ><TR
131 ><TD
132 ><PRE
133 CLASS="PROGRAMLISTING"
134 > 1&nbsp;#!/bin/bash
135 2&nbsp;
136 3&nbsp;for i in {1..10}
137 4&nbsp;# Simpler and more straightforward than
138 5&nbsp;#+ for i in $(seq 10)
139 6&nbsp;do
140 7&nbsp; echo -n "$i "
141 8&nbsp;done
142 9&nbsp;
143 10&nbsp;echo
144 11&nbsp;
145 12&nbsp;# 1 2 3 4 5 6 7 8 9 10
146 13&nbsp;
147 14&nbsp;
148 15&nbsp;
149 16&nbsp;# Or just . . .
150 17&nbsp;
151 18&nbsp;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
152 19&nbsp;echo {e..m} # e f g h i j k l m
153 20&nbsp;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
154 21&nbsp; # Works backwards, too.
155 22&nbsp;echo {25..30} # 25 26 27 28 29 30
156 23&nbsp;echo {3..-2} # 3 2 1 0 -1 -2
157 24&nbsp;echo {X..d} # X Y Z [ ] ^ _ ` a b c d
158 25&nbsp; # Shows (some of) the ASCII characters between Z and a,
159 26&nbsp; #+ but don't rely on this type of behavior because . . .
160 27&nbsp;echo {]..a} # {]..a}
161 28&nbsp; # Why?
162 29&nbsp;
163 30&nbsp;
164 31&nbsp;# You can tack on prefixes and suffixes.
165 32&nbsp;echo "Number #"{1..4}, "..."
166 33&nbsp; # Number #1, Number #2, Number #3, Number #4, ...
167 34&nbsp;
168 35&nbsp;
169 36&nbsp;# You can concatenate brace-expansion sets.
170 37&nbsp;echo {1..3}{x..z}" +" "..."
171 38&nbsp; # 1x + 1y + 1z + 2x + 2y + 2z + 3x + 3y + 3z + ...
172 39&nbsp; # Generates an algebraic expression.
173 40&nbsp; # This could be used to find permutations.
174 41&nbsp;
175 42&nbsp;# You can nest brace-expansion sets.
176 43&nbsp;echo {{a..c},{1..3}}
177 44&nbsp; # a b c 1 2 3
178 45&nbsp; # The "comma operator" splices together strings.
179 46&nbsp;
180 47&nbsp;# ########## ######### ############ ########### ######### ###############
181 48&nbsp;# Unfortunately, brace expansion does not lend itself to parameterization.
182 49&nbsp;var1=1
183 50&nbsp;var2=5
184 51&nbsp;echo {$var1..$var2} # {1..5}
185 52&nbsp;
186 53&nbsp;
187 54&nbsp;# Yet, as Emiliano G. points out, using "eval" overcomes this limitation.
188 55&nbsp;
189 56&nbsp;start=0
190 57&nbsp;end=10
191 58&nbsp;for index in $(eval echo {$start..$end})
192 59&nbsp;do
193 60&nbsp; echo -n "$index " # 0 1 2 3 4 5 6 7 8 9 10
194 61&nbsp;done
195 62&nbsp;
196 63&nbsp;echo</PRE
197 ></TD
198 ></TR
199 ></TABLE
200 ></P
201 ></LI
202 ><LI
203 ><P
204 >The <B
205 CLASS="COMMAND"
206 >${!array[@]}</B
207 > operator, which
208 expands to all the indices of a given <A
209 HREF="arrays.html#ARRAYREF"
210 >array</A
211 >.</P
212 ><P
213 ><TABLE
214 BORDER="0"
215 BGCOLOR="#E0E0E0"
216 WIDTH="90%"
217 ><TR
218 ><TD
219 ><PRE
220 CLASS="PROGRAMLISTING"
221 > 1&nbsp;#!/bin/bash
222 2&nbsp;
223 3&nbsp;Array=(element-zero element-one element-two element-three)
224 4&nbsp;
225 5&nbsp;echo ${Array[0]} # element-zero
226 6&nbsp; # First element of array.
227 7&nbsp;
228 8&nbsp;echo ${!Array[@]} # 0 1 2 3
229 9&nbsp; # All the indices of Array.
230 10&nbsp;
231 11&nbsp;for i in ${!Array[@]}
232 12&nbsp;do
233 13&nbsp; echo ${Array[i]} # element-zero
234 14&nbsp; # element-one
235 15&nbsp; # element-two
236 16&nbsp; # element-three
237 17&nbsp; #
238 18&nbsp; # All the elements in Array.
239 19&nbsp;done</PRE
240 ></TD
241 ></TR
242 ></TABLE
243 ></P
244 ></LI
245 ><LI
246 ><P
247 ><A
248 NAME="REGEXMATCHREF"
249 ></A
250 ></P
251 ><P
252 >The <B
253 CLASS="COMMAND"
254 >=~</B
255 > <A
256 HREF="regexp.html#REGEXREF"
257 >Regular
258 Expression</A
259 > matching operator within a <A
260 HREF="tests.html#DBLBRACKETS"
261 >double brackets</A
262 > test expression.
263 (Perl has a similar operator.)</P
264 ><P
265 ><TABLE
266 BORDER="0"
267 BGCOLOR="#E0E0E0"
268 WIDTH="90%"
269 ><TR
270 ><TD
271 ><PRE
272 CLASS="PROGRAMLISTING"
273 > 1&nbsp;#!/bin/bash
274 2&nbsp;
275 3&nbsp;variable="This is a fine mess."
276 4&nbsp;
277 5&nbsp;echo "$variable"
278 6&nbsp;
279 7&nbsp;# Regex matching with =~ operator within [[ double brackets ]].
280 8&nbsp;if [[ "$variable" =~ T.........fin*es* ]]
281 9&nbsp;# NOTE: As of version 3.2 of Bash, expression to match no longer quoted.
282 10&nbsp;then
283 11&nbsp; echo "match found"
284 12&nbsp; # match found
285 13&nbsp;fi</PRE
286 ></TD
287 ></TR
288 ></TABLE
289 ></P
290 ><P
291 >Or, more usefully:</P
292 ><P
293 ><TABLE
294 BORDER="0"
295 BGCOLOR="#E0E0E0"
296 WIDTH="90%"
297 ><TR
298 ><TD
299 ><PRE
300 CLASS="PROGRAMLISTING"
301 > 1&nbsp;#!/bin/bash
302 2&nbsp;
303 3&nbsp;input=$1
304 4&nbsp;
305 5&nbsp;
306 6&nbsp;if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
307 7&nbsp;# ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.
308 8&nbsp;# NNN-NN-NNNN (where each N is a digit).
309 9&nbsp;then
310 10&nbsp; echo "Social Security number."
311 11&nbsp; # Process SSN.
312 12&nbsp;else
313 13&nbsp; echo "Not a Social Security number!"
314 14&nbsp; # Or, ask for corrected input.
315 15&nbsp;fi</PRE
316 ></TD
317 ></TR
318 ></TABLE
319 ></P
320 ><P
321 >For additional examples of using the
322 <B
323 CLASS="COMMAND"
324 >=~</B
325 > operator, see <A
326 HREF="contributed-scripts.html#WHX"
327 >Example A-29</A
328 >,
329 <A
330 HREF="here-docs.html#MAILBOXGREP"
331 >Example 19-14</A
332 >, <A
333 HREF="contributed-scripts.html#FINDSPLIT"
334 >Example A-35</A
335 >, and <A
336 HREF="contributed-scripts.html#TOHTML"
337 >Example A-24</A
338 >.</P
339 ></LI
340 ><LI
341 ><P
342 ><A
343 NAME="PIPEFAILREF"
344 ></A
345 ></P
346 ><P
347 >The new <TT
348 CLASS="OPTION"
349 >set -o pipefail</TT
350 > option is
351 useful for debugging <A
352 HREF="special-chars.html#PIPEREF"
353 >pipes</A
354 >. If
355 this option is set, then the <A
356 HREF="exit-status.html#EXITSTATUSREF"
357 >exit status</A
358 > of a pipe
359 is the exit status of the last command in the pipe to
360 <SPAN
361 CLASS="emphasis"
362 ><I
363 CLASS="EMPHASIS"
364 >fail</I
365 ></SPAN
366 > (return a non-zero value), rather
367 than the actual final command in the pipe.</P
368 ><P
369 >See <A
370 HREF="communications.html#FC4UPD"
371 >Example 16-43</A
372 >.</P
373 ></LI
374 ></UL
375 >
376 </P
377 ><DIV
378 CLASS="CAUTION"
379 ><TABLE
380 CLASS="CAUTION"
381 WIDTH="100%"
382 BORDER="0"
383 ><TR
384 ><TD
385 WIDTH="25"
386 ALIGN="CENTER"
387 VALIGN="TOP"
388 ><IMG
389 SRC="common/caution.png"
390 HSPACE="5"
391 ALT="Caution"></TD
392 ><TD
393 ALIGN="LEFT"
394 VALIGN="TOP"
395 ><P
396 >The update to version 3 of Bash breaks a few scripts
397 that worked under earlier versions. <SPAN
398 CLASS="emphasis"
399 ><I
400 CLASS="EMPHASIS"
401 >Test critical legacy
402 scripts to make sure they still work!</I
403 ></SPAN
404 ></P
405 ><P
406 >As it happens, a couple of the scripts in the
407 <SPAN
408 CLASS="emphasis"
409 ><I
410 CLASS="EMPHASIS"
411 >Advanced Bash Scripting Guide</I
412 ></SPAN
413 > had to be
414 fixed up (see <A
415 HREF="variables2.html#TOUT"
416 >Example 9-4</A
417 >, for instance).</P
418 ></TD
419 ></TR
420 ></TABLE
421 ></DIV
422 ><DIV
423 CLASS="SECT2"
424 ><H2
425 CLASS="SECT2"
426 ><A
427 NAME="AEN20956"
428 ></A
429 >37.2.1. Bash, version 3.1</H2
430 ><P
431 >The version 3.1 update of Bash introduces a number of bugfixes
432 and a few minor changes.</P
433 ><UL
434 ><LI
435 ><P
436 >The <SPAN
437 CLASS="TOKEN"
438 >+=</SPAN
439 > operator is now permitted in
440 in places where previously only the <SPAN
441 CLASS="TOKEN"
442 >=</SPAN
443 >
444 assignment operator was recognized.</P
445 ><P
446 ><A
447 NAME="PLUSEQSTR"
448 ></A
449 ></P
450 ><P
451 ><TABLE
452 BORDER="0"
453 BGCOLOR="#E0E0E0"
454 WIDTH="90%"
455 ><TR
456 ><TD
457 ><PRE
458 CLASS="PROGRAMLISTING"
459 > 1&nbsp;a=1
460 2&nbsp;echo $a # 1
461 3&nbsp;
462 4&nbsp;a+=5 # Won't work under versions of Bash earlier than 3.1.
463 5&nbsp;echo $a # 15
464 6&nbsp;
465 7&nbsp;a+=Hello
466 8&nbsp;echo $a # 15Hello</PRE
467 ></TD
468 ></TR
469 ></TABLE
470 >
471 </P
472 ><P
473 >Here, <SPAN
474 CLASS="TOKEN"
475 >+=</SPAN
476 > functions as a <I
477 CLASS="FIRSTTERM"
478 >string
479 concatenation</I
480 > operator. Note that its behavior
481 in this particular context is different than within a
482 <A
483 HREF="internal.html#LETREF"
484 >let</A
485 > construct.</P
486 ><P
487 ><TABLE
488 BORDER="0"
489 BGCOLOR="#E0E0E0"
490 WIDTH="90%"
491 ><TR
492 ><TD
493 ><PRE
494 CLASS="PROGRAMLISTING"
495 > 1&nbsp;a=1
496 2&nbsp;echo $a # 1
497 3&nbsp;
498 4&nbsp;let a+=5 # Integer arithmetic, rather than string concatenation.
499 5&nbsp;echo $a # 6
500 6&nbsp;
501 7&nbsp;let a+=Hello # Doesn't "add" anything to a.
502 8&nbsp;echo $a # 6</PRE
503 ></TD
504 ></TR
505 ></TABLE
506 >
507 </P
508 ><P
509 ><A
510 NAME="PATHAPPEND"
511 ></A
512 >Jeffrey Haemer points out
513 that this concatenation operator can be quite
514 useful. In this instance, we append a directory to the
515 <TT
516 CLASS="VARNAME"
517 >$PATH</TT
518 >.</P
519 ><P
520 >&#13; <TABLE
521 BORDER="0"
522 BGCOLOR="#E0E0E0"
523 WIDTH="90%"
524 ><TR
525 ><TD
526 ><PRE
527 CLASS="SCREEN"
528 > <TT
529 CLASS="PROMPT"
530 >bash$ </TT
531 ><TT
532 CLASS="USERINPUT"
533 ><B
534 >echo $PATH</B
535 ></TT
536 >
537 <TT
538 CLASS="COMPUTEROUTPUT"
539 >/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games</TT
540 >
541
542
543 <TT
544 CLASS="PROMPT"
545 >bash$ </TT
546 ><TT
547 CLASS="USERINPUT"
548 ><B
549 >PATH+=:/opt/bin</B
550 ></TT
551 >
552
553 <TT
554 CLASS="PROMPT"
555 >bash$ </TT
556 ><TT
557 CLASS="USERINPUT"
558 ><B
559 >echo $PATH</B
560 ></TT
561 >
562 <TT
563 CLASS="COMPUTEROUTPUT"
564 >/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games:/opt/bin</TT
565 >
566 </PRE
567 ></TD
568 ></TR
569 ></TABLE
570 >
571
572 </P
573 ></LI
574 ></UL
575 ></DIV
576 ><DIV
577 CLASS="SECT2"
578 ><H2
579 CLASS="SECT2"
580 ><A
581 NAME="AEN20987"
582 ></A
583 >37.2.2. Bash, version 3.2</H2
584 ><P
585 >This is pretty much a bugfix update.</P
586 ><UL
587 ><LI
588 ><P
589 >In <A
590 HREF="parameter-substitution.html#PSGLOB"
591 ><I
592 CLASS="FIRSTTERM"
593 >global</I
594 >
595 parameter substitutions</A
596 >, the pattern no longer anchors
597 at the start of the string.</P
598 ></LI
599 ><LI
600 ><P
601 >The <TT
602 CLASS="OPTION"
603 >--wordexp</TT
604 > option disables
605 <A
606 HREF="process-sub.html#PROCESSSUBREF"
607 >process substitution</A
608 >.</P
609 ></LI
610 ><LI
611 ><P
612 >The <B
613 CLASS="COMMAND"
614 >=~</B
615 > <A
616 HREF="bashver3.html#REGEXMATCHREF"
617 >Regular Expression
618 match operator</A
619 > no longer requires
620 <A
621 HREF="quoting.html#QUOTINGREF"
622 >quoting</A
623 > of the
624 <I
625 CLASS="FIRSTTERM"
626 >pattern</I
627 > within <A
628 HREF="tests.html#DBLBRACKETS"
629 >[[ ... ]]</A
630 >.</P
631 ><DIV
632 CLASS="CAUTION"
633 ><TABLE
634 CLASS="CAUTION"
635 WIDTH="90%"
636 BORDER="0"
637 ><TR
638 ><TD
639 WIDTH="25"
640 ALIGN="CENTER"
641 VALIGN="TOP"
642 ><IMG
643 SRC="common/caution.png"
644 HSPACE="5"
645 ALT="Caution"></TD
646 ><TD
647 ALIGN="LEFT"
648 VALIGN="TOP"
649 ><P
650 >In fact, quoting in this context is
651 <SPAN
652 CLASS="emphasis"
653 ><I
654 CLASS="EMPHASIS"
655 >not</I
656 ></SPAN
657 > advisable as it may
658 cause <I
659 CLASS="FIRSTTERM"
660 >regex</I
661 > evaluation to fail.
662 Chet Ramey states in the <A
663 HREF="biblio.html#BASHFAQ"
664 >Bash
665 FAQ</A
666 > that quoting explicitly disables regex evaluation.
667 See also the <A
668 HREF="https://bugs.launchpad.net/ubuntu-website/+bug/109931"
669 TARGET="_top"
670 > Ubuntu Bug List</A
671 > and <A
672 HREF="http://en.wikinerds.org/index.php/Bash_syntax_and_semantics"
673 TARGET="_top"
674 > Wikinerds on Bash syntax</A
675 >.</P
676 ><P
677 >Setting <SPAN
678 CLASS="emphasis"
679 ><I
680 CLASS="EMPHASIS"
681 >shopt -s compat31</I
682 ></SPAN
683 >
684 in a script causes reversion to the original
685 behavior.</P
686 ></TD
687 ></TR
688 ></TABLE
689 ></DIV
690 ></LI
691 ></UL
692 ></DIV
693 ></DIV
694 ><DIV
695 CLASS="NAVFOOTER"
696 ><HR
697 ALIGN="LEFT"
698 WIDTH="100%"><TABLE
699 SUMMARY="Footer navigation table"
700 WIDTH="100%"
701 BORDER="0"
702 CELLPADDING="0"
703 CELLSPACING="0"
704 ><TR
705 ><TD
706 WIDTH="33%"
707 ALIGN="left"
708 VALIGN="top"
709 ><A
710 HREF="bash2.html"
711 ACCESSKEY="P"
712 >Prev</A
713 ></TD
714 ><TD
715 WIDTH="34%"
716 ALIGN="center"
717 VALIGN="top"
718 ><A
719 HREF="index.html"
720 ACCESSKEY="H"
721 >Home</A
722 ></TD
723 ><TD
724 WIDTH="33%"
725 ALIGN="right"
726 VALIGN="top"
727 ><A
728 HREF="bashver4.html"
729 ACCESSKEY="N"
730 >Next</A
731 ></TD
732 ></TR
733 ><TR
734 ><TD
735 WIDTH="33%"
736 ALIGN="left"
737 VALIGN="top"
738 >Bash, versions 2, 3, and 4</TD
739 ><TD
740 WIDTH="34%"
741 ALIGN="center"
742 VALIGN="top"
743 ><A
744 HREF="bash2.html"
745 ACCESSKEY="U"
746 >Up</A
747 ></TD
748 ><TD
749 WIDTH="33%"
750 ALIGN="right"
751 VALIGN="top"
752 >Bash, version 4</TD
753 ></TR
754 ></TABLE
755 ></DIV
756 ></BODY
757 ></HTML
758 >