1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5
>Bash, version 3</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
11
TITLE="Advanced Bash-Scripting Guide"
12
HREF="index.html"><LINK
14
TITLE="Bash, versions 2, 3, and 4"
15
HREF="bash2.html"><LINK
17
TITLE="Bash, versions 2, 3, and 4"
18
HREF="bash2.html"><LINK
20
TITLE="Bash, version 4"
21
HREF="bashver4.html"><META
22
HTTP-EQUIV="Content-Style-Type"
23
CONTENT="text/css"><LINK
25
HREF="common/kde-common.css"
27
HTTP-EQUIV="Content-Type"
28
CONTENT="text/html; charset=iso-8859-1"><META
29
HTTP-EQUIV="Content-Language"
32
HREF="common/kde-localised.css"
34
TITLE="KDE-English"><LINK
36
HREF="common/kde-default.css"
38
TITLE="KDE-Default"></HEAD
46
STYLE="font-family: sans-serif;"
50
SUMMARY="Header navigation table"
59
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
75
>Chapter 36. Bash, versions 2, 3, and 4</TD
97
>36.2. Bash, version 3</H1
104
>On July 27, 2004, Chet Ramey released version 3 of Bash.
105
This update fixed quite a number of bugs and added new
108
>Some of the more important added features:
118
>A new, more generalized <B
122
HREF="special-chars.html#BRACEEXPREF"
133
CLASS="PROGRAMLISTING"
136
3 for i in {1..10}
137
4 # Simpler and more straightforward than
138
5 #+ for i in $(seq 10)
140
7 echo -n "$i "
145
12 # 1 2 3 4 5 6 7 8 9 10
149
16 # Or just . . .
151
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
152
19 echo {e..m} # e f g h i j k l m
153
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
154
21 # Works backwards, too.
155
22 echo {25..30} # 25 26 27 28 29 30
156
23 echo {3..-2} # 3 2 1 0 -1 -2
157
24 echo {X..d} # X Y Z [ ] ^ _ ` a b c d
158
25 # Shows (some of) the ASCII characters between Z and a,
159
26 #+ but don't rely on this type of behavior because . . .
160
27 echo {]..a} # {]..a}
164
31 # You can tack on prefixes and suffixes.
165
32 echo "Number #"{1..4}, "..."
166
33 # Number #1, Number #2, Number #3, Number #4, ...
169
36 # You can concatenate brace-expansion sets.
170
37 echo {1..3}{x..z}" +" "..."
171
38 # 1x + 1y + 1z + 2x + 2y + 2z + 3x + 3y + 3z + ...
172
39 # Generates an algebraic expression.
173
40 # This could be used to find permutations.
175
42 # You can nest brace-expansion sets.
176
43 echo {{a..c},{1..3}}
177
44 # a b c 1 2 3
178
45 # The "comma operator" splices together strings.
181
48 # Unfortunately, brace expansion does not lend itself to parameterization.
184
51 echo {$var1..$var2} # {1..5}</PRE
196
expands to all the indices of a given <A
197
HREF="arrays.html#ARRAYREF"
208
CLASS="PROGRAMLISTING"
211
3 Array=(element-zero element-one element-two element-three)
213
5 echo ${Array[0]} # element-zero
214
6 # First element of array.
216
8 echo ${!Array[@]} # 0 1 2 3
217
9 # All the indices of Array.
219
11 for i in ${!Array[@]}
221
13 echo ${Array[i]} # element-zero
222
14 # element-one
223
15 # element-two
224
16 # element-three
226
18 # All the elements in Array.
244
HREF="regexp.html#REGEXREF"
247
> matching operator within a <A
248
HREF="tests.html#DBLBRACKETS"
251
(Perl has a similar operator.)</P
260
CLASS="PROGRAMLISTING"
263
3 variable="This is a fine mess."
265
5 echo "$variable"
267
7 # Regex matching with =~ operator within [[ double brackets ]].
268
8 if [[ "$variable" =~ T.........fin*es* ]]
269
9 # NOTE: As of version 3.2 of Bash, expression to match no longer quoted.
271
11 echo "match found"
272
12 # match found
279
>Or, more usefully:</P
288
CLASS="PROGRAMLISTING"
294
6 if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
295
7 # ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.
296
8 # NNN-NN-NNNN (where each N is a digit).
298
10 echo "Social Security number."
299
11 # Process SSN.
301
13 echo "Not a Social Security number!"
302
14 # Or, ask for corrected input.
309
>For additional examples of using the
314
HREF="contributed-scripts.html#WHX"
318
HREF="here-docs.html#MAILBOXGREP"
321
HREF="contributed-scripts.html#FINDSPLIT"
324
HREF="contributed-scripts.html#TOHTML"
339
useful for debugging <A
340
HREF="special-chars.html#PIPEREF"
343
this option is set, then the <A
344
HREF="exit-status.html#EXITSTATUSREF"
347
is the exit status of the last command in the pipe to
354
> (return a non-zero value), rather
355
than the actual final command in the pipe.</P
358
HREF="communications.html#FC4UPD"
377
SRC="common/caution.png"
384
>The update to version 3 of Bash breaks a few scripts
385
that worked under earlier versions. <SPAN
389
>Test critical legacy
390
scripts to make sure they still work!</I
394
>As it happens, a couple of the scripts in the
399
>Advanced Bash Scripting Guide</I
403
HREF="variables2.html#TOUT"
417
>36.2.1. Bash, version 3.1</H2
419
>The version 3.1 update of Bash introduces a number of bugfixes
420
and a few minor changes.</P
427
> operator is now permitted in
428
in places where previously only the <SPAN
432
assignment operator was recognized.</P
446
CLASS="PROGRAMLISTING"
450
4 a+=5 # Won't work under versions of Bash earlier than 3.1.
454
8 echo $a # 15Hello</PRE
468
> operator. Note that its behavior
469
in this particular context is different than within a
471
HREF="internal.html#LETREF"
482
CLASS="PROGRAMLISTING"
486
4 let a+=5 # Integer arithmetic, rather than string concatenation.
489
7 let a+=Hello # Doesn't "add" anything to a.
490
8 echo $a # 6</PRE
506
>36.2.2. Bash, version 3.2</H2
508
>This is pretty much a bugfix update.</P
513
HREF="parameter-substitution.html#PSGLOB"
518
parameter substitutions</A
519
>, the pattern no longer anchors
520
at the start of the string.</P
529
HREF="process-sub.html#PROCESSSUBREF"
530
>process substitution</A
539
HREF="bashver3.html#REGEXMATCHREF"
544
HREF="quoting.html#QUOTINGREF"
551
HREF="tests.html#DBLBRACKETS"
566
SRC="common/caution.png"
573
>In fact, quoting in this context is
580
> advisable as it may
584
> evaluation to fail.
585
Chet Ramey states in the <A
586
HREF="biblio.html#BASHFAQ"
589
> that quoting explicitly disables regex evaluation.
591
HREF="https://bugs.launchpad.net/ubuntu-website/+bug/109931"
595
HREF="http://en.wikinerds.org/index.php/Bash_syntax_and_semantics"
597
> Wikinerds on Bash syntax</A
604
>shopt -s compat31</I
607
in a script causes reversion to the original
622
SUMMARY="Footer navigation table"
661
>Bash, versions 2, 3, and 4</TD
b'\\ No newline at end of file'