File: bashver3.html

package info (click to toggle)
abs-guide 10-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 6,952 kB
  • sloc: sh: 14,129; makefile: 81
file content (758 lines) | stat: -rw-r--r-- 13,366 bytes parent folder | download | duplicates (3)
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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;for i in {1..10}
   4&nbsp;#  Simpler and more straightforward than
   5&nbsp;#+ for i in $(seq 10)
   6&nbsp;do
   7&nbsp;  echo -n "$i "
   8&nbsp;done
   9&nbsp;
  10&nbsp;echo
  11&nbsp;
  12&nbsp;# 1 2 3 4 5 6 7 8 9 10
  13&nbsp;
  14&nbsp;
  15&nbsp;
  16&nbsp;# Or just . . .
  17&nbsp;
  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
  19&nbsp;echo {e..m}    #  e f g h i j k l m
  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
  21&nbsp;               #  Works backwards, too.
  22&nbsp;echo {25..30}  #  25 26 27 28 29 30
  23&nbsp;echo {3..-2}   #  3 2 1 0 -1 -2
  24&nbsp;echo {X..d}    #  X Y Z [  ] ^ _ ` a b c d
  25&nbsp;               #  Shows (some of) the ASCII characters between Z and a,
  26&nbsp;               #+ but don't rely on this type of behavior because . . .
  27&nbsp;echo {]..a}    #  {]..a}
  28&nbsp;               #  Why?
  29&nbsp;
  30&nbsp;
  31&nbsp;# You can tack on prefixes and suffixes.
  32&nbsp;echo "Number #"{1..4}, "..."
  33&nbsp;     # Number #1, Number #2, Number #3, Number #4, ...
  34&nbsp;
  35&nbsp;
  36&nbsp;# You can concatenate brace-expansion sets.
  37&nbsp;echo {1..3}{x..z}" +" "..."
  38&nbsp;     # 1x + 1y + 1z + 2x + 2y + 2z + 3x + 3y + 3z + ...
  39&nbsp;     # Generates an algebraic expression.
  40&nbsp;     # This could be used to find permutations.
  41&nbsp;
  42&nbsp;# You can nest brace-expansion sets.
  43&nbsp;echo {{a..c},{1..3}}
  44&nbsp;     # a b c 1 2 3
  45&nbsp;     # The "comma operator" splices together strings.
  46&nbsp;
  47&nbsp;# ########## ######### ############ ########### ######### ###############
  48&nbsp;# Unfortunately, brace expansion does not lend itself to parameterization.
  49&nbsp;var1=1
  50&nbsp;var2=5
  51&nbsp;echo {$var1..$var2}   # {1..5}
  52&nbsp;
  53&nbsp;
  54&nbsp;# Yet, as Emiliano G. points out, using "eval" overcomes this limitation.
  55&nbsp;
  56&nbsp;start=0
  57&nbsp;end=10
  58&nbsp;for index in $(eval echo {$start..$end})
  59&nbsp;do
  60&nbsp;  echo -n "$index "   # 0 1 2 3 4 5 6 7 8 9 10 
  61&nbsp;done
  62&nbsp;
  63&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;Array=(element-zero element-one element-two element-three)
   4&nbsp;
   5&nbsp;echo ${Array[0]}   # element-zero
   6&nbsp;                   # First element of array.
   7&nbsp;
   8&nbsp;echo ${!Array[@]}  # 0 1 2 3
   9&nbsp;                   # All the indices of Array.
  10&nbsp;
  11&nbsp;for i in ${!Array[@]}
  12&nbsp;do
  13&nbsp;  echo ${Array[i]} # element-zero
  14&nbsp;                   # element-one
  15&nbsp;                   # element-two
  16&nbsp;                   # element-three
  17&nbsp;                   #
  18&nbsp;                   # All the elements in Array.
  19&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;variable="This is a fine mess."
   4&nbsp;
   5&nbsp;echo "$variable"
   6&nbsp;
   7&nbsp;# Regex matching with =~ operator within [[ double brackets ]].
   8&nbsp;if [[ "$variable" =~ T.........fin*es* ]]
   9&nbsp;# NOTE: As of version 3.2 of Bash, expression to match no longer quoted.
  10&nbsp;then
  11&nbsp;  echo "match found"
  12&nbsp;      # match found
  13&nbsp;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&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;input=$1
   4&nbsp;
   5&nbsp;
   6&nbsp;if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
   7&nbsp;#                 ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.
   8&nbsp;# NNN-NN-NNNN (where each N is a digit).
   9&nbsp;then
  10&nbsp;  echo "Social Security number."
  11&nbsp;  # Process SSN.
  12&nbsp;else
  13&nbsp;  echo "Not a Social Security number!"
  14&nbsp;  # Or, ask for corrected input.
  15&nbsp;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&nbsp;a=1
   2&nbsp;echo $a        # 1
   3&nbsp;
   4&nbsp;a+=5           # Won't work under versions of Bash earlier than 3.1.
   5&nbsp;echo $a        # 15
   6&nbsp;
   7&nbsp;a+=Hello
   8&nbsp;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&nbsp;a=1
   2&nbsp;echo $a        # 1
   3&nbsp;
   4&nbsp;let a+=5       # Integer arithmetic, rather than string concatenation.
   5&nbsp;echo $a        # 6
   6&nbsp;
   7&nbsp;let a+=Hello   # Doesn't "add" anything to a.
   8&nbsp;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
>&#13;      <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
>