Debian Codegraph @

HTML/bashver4.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Bash, version 4</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, version 3"
18 HREF="bashver3.html"><LINK
19 REL="NEXT"
20 TITLE="Endnotes"
21 HREF="endnotes.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="bashver3.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="endnotes.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="BASHVER4"
96 ></A
97 >37.3. Bash, version 4</H1
98 ><P
99 ><A
100 NAME="BASH4REF"
101 ></A
102 ></P
103 ><P
104 >Chet Ramey announced Version 4 of Bash on the 20th
105 of February, 2009. This release has a number of significant
106 new features, as well as some important bugfixes.</P
107 ><P
108 >Among the new goodies:</P
109 ><UL
110 ><LI
111 ><P
112 ><A
113 NAME="ASSOCARR"
114 ></A
115 >Associative arrays.
116 <A
117 NAME="AEN21025"
118 HREF="#FTN.AEN21025"
119 >[1]</A
120 >
121 </P
122 ><TABLE
123 CLASS="SIDEBAR"
124 BORDER="1"
125 CELLPADDING="5"
126 ><TR
127 ><TD
128 ><DIV
129 CLASS="SIDEBAR"
130 ><A
131 NAME="AEN21029"
132 ></A
133 ><P
134 >An <I
135 CLASS="FIRSTTERM"
136 >associative</I
137 > array can
138 be thought of as a set of two linked arrays -- one holding
139 the <I
140 CLASS="FIRSTTERM"
141 >data</I
142 >, and the other the
143 <I
144 CLASS="FIRSTTERM"
145 >keys</I
146 > that index the individual elements
147 of the <I
148 CLASS="FIRSTTERM"
149 >data</I
150 > array.</P
151 ></DIV
152 ></TD
153 ></TR
154 ></TABLE
155 ><DIV
156 CLASS="EXAMPLE"
157 ><HR><A
158 NAME="FETCHADDRESS"
159 ></A
160 ><P
161 ><B
162 >Example 37-5. A simple address database</B
163 ></P
164 ><TABLE
165 BORDER="0"
166 BGCOLOR="#E0E0E0"
167 WIDTH="90%"
168 ><TR
169 ><TD
170 ><PRE
171 CLASS="PROGRAMLISTING"
172 > 1&nbsp;#!/bin/bash4
173 2&nbsp;# fetch_address.sh
174 3&nbsp;
175 4&nbsp;declare -A address
176 5&nbsp;# -A option declares associative array.
177 6&nbsp;
178 7&nbsp;address[Charles]="414 W. 10th Ave., Baltimore, MD 21236"
179 8&nbsp;address[John]="202 E. 3rd St., New York, NY 10009"
180 9&nbsp;address[Wilma]="1854 Vermont Ave, Los Angeles, CA 90023"
181 10&nbsp;
182 11&nbsp;
183 12&nbsp;echo "Charles's address is ${address[Charles]}."
184 13&nbsp;# Charles's address is 414 W. 10th Ave., Baltimore, MD 21236.
185 14&nbsp;echo "Wilma's address is ${address[Wilma]}."
186 15&nbsp;# Wilma's address is 1854 Vermont Ave, Los Angeles, CA 90023.
187 16&nbsp;echo "John's address is ${address[John]}."
188 17&nbsp;# John's address is 202 E. 3rd St., New York, NY 10009.
189 18&nbsp;
190 19&nbsp;echo
191 20&nbsp;
192 21&nbsp;echo "${!address[*]}" # The array indices ...
193 22&nbsp;# Charles John Wilma</PRE
194 ></TD
195 ></TR
196 ></TABLE
197 ><HR></DIV
198 ><DIV
199 CLASS="EXAMPLE"
200 ><HR><A
201 NAME="FETCHADDRESS2"
202 ></A
203 ><P
204 ><B
205 >Example 37-6. A somewhat more elaborate address database</B
206 ></P
207 ><TABLE
208 BORDER="0"
209 BGCOLOR="#E0E0E0"
210 WIDTH="90%"
211 ><TR
212 ><TD
213 ><PRE
214 CLASS="PROGRAMLISTING"
215 > 1&nbsp;#!/bin/bash4
216 2&nbsp;# fetch_address-2.sh
217 3&nbsp;# A more elaborate version of fetch_address.sh.
218 4&nbsp;
219 5&nbsp;SUCCESS=0
220 6&nbsp;E_DB=99 # Error code for missing entry.
221 7&nbsp;
222 8&nbsp;declare -A address
223 9&nbsp;# -A option declares associative array.
224 10&nbsp;
225 11&nbsp;
226 12&nbsp;store_address ()
227 13&nbsp;{
228 14&nbsp; address[$1]="$2"
229 15&nbsp; return $?
230 16&nbsp;}
231 17&nbsp;
232 18&nbsp;
233 19&nbsp;fetch_address ()
234 20&nbsp;{
235 21&nbsp; if [[ -z "${address[$1]}" ]]
236 22&nbsp; then
237 23&nbsp; echo "$1's address is not in database."
238 24&nbsp; return $E_DB
239 25&nbsp; fi
240 26&nbsp;
241 27&nbsp; echo "$1's address is ${address[$1]}."
242 28&nbsp; return $?
243 29&nbsp;}
244 30&nbsp;
245 31&nbsp;
246 32&nbsp;store_address "Lucas Fayne" "414 W. 13th Ave., Baltimore, MD 21236"
247 33&nbsp;store_address "Arvid Boyce" "202 E. 3rd St., New York, NY 10009"
248 34&nbsp;store_address "Velma Winston" "1854 Vermont Ave, Los Angeles, CA 90023"
249 35&nbsp;# Exercise:
250 36&nbsp;# Rewrite the above store_address calls to read data from a file,
251 37&nbsp;#+ then assign field 1 to name, field 2 to address in the array.
252 38&nbsp;# Each line in the file would have a format corresponding to the above.
253 39&nbsp;# Use a while-read loop to read from file, sed or awk to parse the fields.
254 40&nbsp;
255 41&nbsp;fetch_address "Lucas Fayne"
256 42&nbsp;# Lucas Fayne's address is 414 W. 13th Ave., Baltimore, MD 21236.
257 43&nbsp;fetch_address "Velma Winston"
258 44&nbsp;# Velma Winston's address is 1854 Vermont Ave, Los Angeles, CA 90023.
259 45&nbsp;fetch_address "Arvid Boyce"
260 46&nbsp;# Arvid Boyce's address is 202 E. 3rd St., New York, NY 10009.
261 47&nbsp;fetch_address "Bozo Bozeman"
262 48&nbsp;# Bozo Bozeman's address is not in database.
263 49&nbsp;
264 50&nbsp;exit $? # In this case, exit code = 99, since that is function return.</PRE
265 ></TD
266 ></TR
267 ></TABLE
268 ><HR></DIV
269 ><P
270 >See <A
271 HREF="contributed-scripts.html#SAMORSE"
272 >Example A-53</A
273 > for an interesting
274 usage of an <I
275 CLASS="FIRSTTERM"
276 >associative array</I
277 >.</P
278 ><DIV
279 CLASS="CAUTION"
280 ><TABLE
281 CLASS="CAUTION"
282 WIDTH="90%"
283 BORDER="0"
284 ><TR
285 ><TD
286 WIDTH="25"
287 ALIGN="CENTER"
288 VALIGN="TOP"
289 ><IMG
290 SRC="common/caution.png"
291 HSPACE="5"
292 ALT="Caution"></TD
293 ><TD
294 ALIGN="LEFT"
295 VALIGN="TOP"
296 ><P
297 >Elements of the <I
298 CLASS="FIRSTTERM"
299 >index</I
300 > array
301 may include embedded <A
302 HREF="special-chars.html#WHITESPACEREF"
303 >space
304 characters</A
305 >, or even leading and/or trailing space
306 characters. However, index array elements containing
307 <SPAN
308 CLASS="emphasis"
309 ><I
310 CLASS="EMPHASIS"
311 >only</I
312 ></SPAN
313 > <I
314 CLASS="FIRSTTERM"
315 >whitespace</I
316 >
317 are <SPAN
318 CLASS="emphasis"
319 ><I
320 CLASS="EMPHASIS"
321 >not</I
322 ></SPAN
323 > permitted.</P
324 ><P
325 ><TABLE
326 BORDER="0"
327 BGCOLOR="#E0E0E0"
328 WIDTH="90%"
329 ><TR
330 ><TD
331 ><PRE
332 CLASS="PROGRAMLISTING"
333 > 1&nbsp;address[ ]="Blank" # Error!</PRE
334 ></TD
335 ></TR
336 ></TABLE
337 >
338 </P
339 ></TD
340 ></TR
341 ></TABLE
342 ></DIV
343 ></LI
344 ><LI
345 ><P
346 ><A
347 NAME="NCTERM"
348 ></A
349 >Enhancements to the
350 <A
351 HREF="testbranch.html#CASEESAC1"
352 >case</A
353 > construct:
354 the <TT
355 CLASS="REPLACEABLE"
356 ><I
357 >;;&#38;</I
358 ></TT
359 > and
360 <TT
361 CLASS="REPLACEABLE"
362 ><I
363 >;&#38;</I
364 ></TT
365 > terminators.</P
366 ><DIV
367 CLASS="EXAMPLE"
368 ><HR><A
369 NAME="CASE4"
370 ></A
371 ><P
372 ><B
373 >Example 37-7. Testing characters</B
374 ></P
375 ><TABLE
376 BORDER="0"
377 BGCOLOR="#E0E0E0"
378 WIDTH="90%"
379 ><TR
380 ><TD
381 ><PRE
382 CLASS="PROGRAMLISTING"
383 > 1&nbsp;#!/bin/bash4
384 2&nbsp;
385 3&nbsp;test_char ()
386 4&nbsp;{
387 5&nbsp; case "$1" in
388 6&nbsp; [[:print:]] ) echo "$1 is a printable character.";;&#38; # |
389 7&nbsp; # The ;;&#38; terminator continues to the next pattern test. |
390 8&nbsp; [[:alnum:]] ) echo "$1 is an alpha/numeric character.";;&#38; # v
391 9&nbsp; [[:alpha:]] ) echo "$1 is an alphabetic character.";;&#38; # v
392 10&nbsp; [[:lower:]] ) echo "$1 is a lowercase alphabetic character.";;&#38;
393 11&nbsp; [[:digit:]] ) echo "$1 is an numeric character.";&#38; # |
394 12&nbsp; # The ;&#38; terminator executes the next statement ... # |
395 13&nbsp; %%%@@@@@ ) echo "********************************";; # v
396 14&nbsp;# ^^^^^^^^ ... even with a dummy pattern.
397 15&nbsp; esac
398 16&nbsp;}
399 17&nbsp;
400 18&nbsp;echo
401 19&nbsp;
402 20&nbsp;test_char 3
403 21&nbsp;# 3 is a printable character.
404 22&nbsp;# 3 is an alpha/numeric character.
405 23&nbsp;# 3 is an numeric character.
406 24&nbsp;# ********************************
407 25&nbsp;echo
408 26&nbsp;
409 27&nbsp;test_char m
410 28&nbsp;# m is a printable character.
411 29&nbsp;# m is an alpha/numeric character.
412 30&nbsp;# m is an alphabetic character.
413 31&nbsp;# m is a lowercase alphabetic character.
414 32&nbsp;echo
415 33&nbsp;
416 34&nbsp;test_char /
417 35&nbsp;# / is a printable character.
418 36&nbsp;
419 37&nbsp;echo
420 38&nbsp;
421 39&nbsp;# The ;;&#38; terminator can save complex if/then conditions.
422 40&nbsp;# The ;&#38; is somewhat less useful.</PRE
423 ></TD
424 ></TR
425 ></TABLE
426 ><HR></DIV
427 ></LI
428 ><LI
429 ><P
430 ><A
431 NAME="COPROCREF"
432 ></A
433 >The new <B
434 CLASS="COMMAND"
435 >coproc</B
436 >
437 builtin enables two parallel <A
438 HREF="special-chars.html#PROCESSREF"
439 >processes</A
440 > to communicate and
441 interact. As Chet Ramey states in the
442 <A
443 HREF="biblio.html#BASHFAQ"
444 >Bash FAQ</A
445 >
446 <A
447 NAME="AEN21068"
448 HREF="#FTN.AEN21068"
449 >[2]</A
450 >
451 , ver. 4.01:
452 </P
453 ><A
454 NAME="AEN21070"
455 ></A
456 ><BLOCKQUOTE
457 CLASS="BLOCKQUOTE"
458 ><P
459 CLASS="LITERALLAYOUT"
460 >&nbsp;&nbsp;&nbsp;&nbsp;There&nbsp;is&nbsp;a&nbsp;new&nbsp;'coproc'&nbsp;reserved&nbsp;word&nbsp;that&nbsp;specifies&nbsp;a&nbsp;coprocess:<br>
461 &nbsp;&nbsp;&nbsp;&nbsp;an&nbsp;asynchronous&nbsp;command&nbsp;run&nbsp;with&nbsp;two&nbsp;pipes&nbsp;connected&nbsp;to&nbsp;the&nbsp;creating<br>
462 &nbsp;&nbsp;&nbsp;&nbsp;shell.&nbsp;Coprocs&nbsp;can&nbsp;be&nbsp;named.&nbsp;The&nbsp;input&nbsp;and&nbsp;output&nbsp;file&nbsp;descriptors<br>
463 &nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;the&nbsp;PID&nbsp;of&nbsp;the&nbsp;coprocess&nbsp;are&nbsp;available&nbsp;to&nbsp;the&nbsp;calling&nbsp;shell&nbsp;in<br>
464 &nbsp;&nbsp;&nbsp;&nbsp;variables&nbsp;with&nbsp;coproc-specific&nbsp;names.<br>
465 <br>
466 &nbsp;&nbsp;&nbsp;&nbsp;George&nbsp;Dimitriu&nbsp;explains,<br>
467 &nbsp;&nbsp;&nbsp;&nbsp;"...&nbsp;coproc&nbsp;...&nbsp;is&nbsp;a&nbsp;feature&nbsp;used&nbsp;in&nbsp;Bash&nbsp;process&nbsp;substitution,<br>
468 &nbsp;&nbsp;&nbsp;&nbsp;which&nbsp;now&nbsp;is&nbsp;made&nbsp;publicly&nbsp;available."<br>
469 &nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;means&nbsp;it&nbsp;can&nbsp;be&nbsp;explicitly&nbsp;invoked&nbsp;in&nbsp;a&nbsp;script,&nbsp;rather&nbsp;than<br>
470 &nbsp;&nbsp;&nbsp;&nbsp;just&nbsp;being&nbsp;a&nbsp;behind-the-scenes&nbsp;mechanism&nbsp;used&nbsp;by&nbsp;Bash.<br>
471 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P
472 ></BLOCKQUOTE
473 ><P
474 >Coprocesses use <I
475 CLASS="FIRSTTERM"
476 >file descriptors</I
477 >.
478 <A
479 HREF="io-redirection.html#FDREF2"
480 >File descriptors enable processes and
481 pipes to communicate</A
482 >.</P
483 ><P
484 ><TABLE
485 BORDER="0"
486 BGCOLOR="#E0E0E0"
487 WIDTH="90%"
488 ><TR
489 ><TD
490 ><PRE
491 CLASS="PROGRAMLISTING"
492 > 1&nbsp;#!/bin/bash4
493 2&nbsp;# A coprocess communicates with a while-read loop.
494 3&nbsp;
495 4&nbsp;
496 5&nbsp;coproc { cat mx_data.txt; sleep 2; }
497 6&nbsp;# ^^^^^^^
498 7&nbsp;# Try running this without "sleep 2" and see what happens.
499 8&nbsp;
500 9&nbsp;while read -u ${COPROC[0]} line # ${COPROC[0]} is the
501 10&nbsp;do #+ file descriptor of the coprocess.
502 11&nbsp; echo "$line" | sed -e 's/line/NOT-ORIGINAL-TEXT/'
503 12&nbsp;done
504 13&nbsp;
505 14&nbsp;kill $COPROC_PID # No longer need the coprocess,
506 15&nbsp; #+ so kill its PID.</PRE
507 ></TD
508 ></TR
509 ></TABLE
510 ></P
511 ><P
512 >But, be careful!</P
513 ><P
514 ><TABLE
515 BORDER="0"
516 BGCOLOR="#E0E0E0"
517 WIDTH="90%"
518 ><TR
519 ><TD
520 ><PRE
521 CLASS="PROGRAMLISTING"
522 > 1&nbsp;#!/bin/bash4
523 2&nbsp;
524 3&nbsp;echo; echo
525 4&nbsp;a=aaa
526 5&nbsp;b=bbb
527 6&nbsp;c=ccc
528 7&nbsp;
529 8&nbsp;coproc echo "one two three"
530 9&nbsp;while read -u ${COPROC[0]} a b c; # Note that this loop
531 10&nbsp;do #+ runs in a subshell.
532 11&nbsp; echo "Inside while-read loop: ";
533 12&nbsp; echo "a = $a"; echo "b = $b"; echo "c = $c"
534 13&nbsp; echo "coproc file descriptor: ${COPROC[0]}"
535 14&nbsp;done
536 15&nbsp;
537 16&nbsp;# a = one
538 17&nbsp;# b = two
539 18&nbsp;# c = three
540 19&nbsp;# So far, so good, but ...
541 20&nbsp;
542 21&nbsp;echo "-----------------"
543 22&nbsp;echo "Outside while-read loop: "
544 23&nbsp;echo "a = $a" # a =
545 24&nbsp;echo "b = $b" # b =
546 25&nbsp;echo "c = $c" # c =
547 26&nbsp;echo "coproc file descriptor: ${COPROC[0]}"
548 27&nbsp;echo
549 28&nbsp;# The coproc is still running, but ...
550 29&nbsp;#+ it still doesn't enable the parent process
551 30&nbsp;#+ to "inherit" variables from the child process, the while-read loop.
552 31&nbsp;
553 32&nbsp;# Compare this to the "badread.sh" script.</PRE
554 ></TD
555 ></TR
556 ></TABLE
557 ></P
558 ><DIV
559 CLASS="CAUTION"
560 ><TABLE
561 CLASS="CAUTION"
562 WIDTH="90%"
563 BORDER="0"
564 ><TR
565 ><TD
566 WIDTH="25"
567 ALIGN="CENTER"
568 VALIGN="TOP"
569 ><IMG
570 SRC="common/caution.png"
571 HSPACE="5"
572 ALT="Caution"></TD
573 ><TD
574 ALIGN="LEFT"
575 VALIGN="TOP"
576 ><P
577 >The coprocess is <I
578 CLASS="FIRSTTERM"
579 >asynchronous</I
580 >,
581 and this might cause a problem. It may terminate before another
582 process has finished communicating with it.</P
583 ><TABLE
584 BORDER="0"
585 BGCOLOR="#E0E0E0"
586 WIDTH="90%"
587 ><TR
588 ><TD
589 ><PRE
590 CLASS="PROGRAMLISTING"
591 > 1&nbsp;#!/bin/bash4
592 2&nbsp;
593 3&nbsp;coproc cpname { for i in {0..10}; do echo "index = $i"; done; }
594 4&nbsp;# ^^^^^^ This is a *named* coprocess.
595 5&nbsp;read -u ${cpname[0]}
596 6&nbsp;echo $REPLY # index = 0
597 7&nbsp;echo ${COPROC[0]} #+ No output ... the coprocess timed out
598 8&nbsp;# after the first loop iteration.
599 9&nbsp;
600 10&nbsp;
601 11&nbsp;
602 12&nbsp;# However, George Dimitriu has a partial fix.
603 13&nbsp;
604 14&nbsp;coproc cpname { for i in {0..10}; do echo "index = $i"; done; sleep 1;
605 15&nbsp;echo hi &#62; myo; cat - &#62;&#62; myo; }
606 16&nbsp;# ^^^^^ This is a *named* coprocess.
607 17&nbsp;
608 18&nbsp;echo "I am main"$'\04' &#62;&#38;${cpname[1]}
609 19&nbsp;myfd=${cpname[0]}
610 20&nbsp;echo myfd=$myfd
611 21&nbsp;
612 22&nbsp;### while read -u $myfd
613 23&nbsp;### do
614 24&nbsp;### echo $REPLY;
615 25&nbsp;### done
616 26&nbsp;
617 27&nbsp;echo $cpname_PID
618 28&nbsp;
619 29&nbsp;# Run this with and without the commented-out while-loop, and it is
620 30&nbsp;#+ apparent that each process, the executing shell and the coprocess,
621 31&nbsp;#+ waits for the other to finish writing in its own write-enabled pipe.</PRE
622 ></TD
623 ></TR
624 ></TABLE
625 ></TD
626 ></TR
627 ></TABLE
628 ></DIV
629 ></LI
630 ><LI
631 ><P
632 ><A
633 NAME="MAPFILEREF"
634 ></A
635 >The new <B
636 CLASS="COMMAND"
637 >mapfile</B
638 >
639 builtin makes it possible to load an array with the contents
640 of a text file without using a loop or <A
641 HREF="arrays.html#ARRAYINITCS"
642 >command substitution</A
643 >.</P
644 ><P
645 ><TABLE
646 BORDER="0"
647 BGCOLOR="#E0E0E0"
648 WIDTH="90%"
649 ><TR
650 ><TD
651 ><PRE
652 CLASS="PROGRAMLISTING"
653 > 1&nbsp;#!/bin/bash4
654 2&nbsp;
655 3&nbsp;mapfile Arr1 &#60; $0
656 4&nbsp;# Same result as Arr1=( $(cat $0) )
657 5&nbsp;echo "${Arr1[@]}" # Copies this entire script out to stdout.
658 6&nbsp;
659 7&nbsp;echo "--"; echo
660 8&nbsp;
661 9&nbsp;# But, not the same as read -a !!!
662 10&nbsp;read -a Arr2 &#60; $0
663 11&nbsp;echo "${Arr2[@]}" # Reads only first line of script into the array.
664 12&nbsp;
665 13&nbsp;exit</PRE
666 ></TD
667 ></TR
668 ></TABLE
669 ></P
670 ></LI
671 ><LI
672 ><P
673 >The <A
674 HREF="internal.html#READREF"
675 >read</A
676 > builtin got
677 a minor facelift. The <TT
678 CLASS="OPTION"
679 >-t</TT
680 >
681 <A
682 HREF="internal.html#READTIMED"
683 >timeout</A
684 > option now accepts
685 (decimal) fractional values
686 <A
687 NAME="AEN21096"
688 HREF="#FTN.AEN21096"
689 >[3]</A
690 >
691 and the <TT
692 CLASS="OPTION"
693 >-i</TT
694 > option
695 permits preloading the edit buffer.
696 <A
697 NAME="AEN21101"
698 HREF="#FTN.AEN21101"
699 >[4]</A
700 >
701 Unfortunately, these enhancements are still a work in progress
702 and not (yet) usable in scripts.</P
703 ></LI
704 ><LI
705 ><P
706 ><A
707 NAME="CASEMODPARAMSUB"
708 ></A
709 >
710 <A
711 HREF="parameter-substitution.html#PARAMSUBREF"
712 >Parameter substitution</A
713 >
714 gets <I
715 CLASS="FIRSTTERM"
716 >case-modification</I
717 > operators.</P
718 ><P
719 ><TABLE
720 BORDER="0"
721 BGCOLOR="#E0E0E0"
722 WIDTH="90%"
723 ><TR
724 ><TD
725 ><PRE
726 CLASS="PROGRAMLISTING"
727 > 1&nbsp;#!/bin/bash4
728 2&nbsp;
729 3&nbsp;var=veryMixedUpVariable
730 4&nbsp;echo ${var} # veryMixedUpVariable
731 5&nbsp;echo ${var^} # VeryMixedUpVariable
732 6&nbsp;# * First char --&#62; uppercase.
733 7&nbsp;echo ${var^^} # VERYMIXEDUPVARIABLE
734 8&nbsp;# ** All chars --&#62; uppercase.
735 9&nbsp;echo ${var,} # veryMixedUpVariable
736 10&nbsp;# * First char --&#62; lowercase.
737 11&nbsp;echo ${var,,} # verymixedupvariable
738 12&nbsp;# ** All chars --&#62; lowercase.</PRE
739 ></TD
740 ></TR
741 ></TABLE
742 ></P
743 ></LI
744 ><LI
745 ><P
746 ><A
747 NAME="DECLARECASEMOD"
748 ></A
749 ></P
750 ><P
751 >The <A
752 HREF="declareref.html"
753 >declare</A
754 > builtin now
755 accepts the <TT
756 CLASS="OPTION"
757 >-l</TT
758 > <I
759 CLASS="FIRSTTERM"
760 >lowercase</I
761 >
762 and <TT
763 CLASS="OPTION"
764 >-c</TT
765 > <I
766 CLASS="FIRSTTERM"
767 >capitalize</I
768 >
769 options.</P
770 ><P
771 ><TABLE
772 BORDER="0"
773 BGCOLOR="#E0E0E0"
774 WIDTH="90%"
775 ><TR
776 ><TD
777 ><PRE
778 CLASS="PROGRAMLISTING"
779 > 1&nbsp;#!/bin/bash4
780 2&nbsp;
781 3&nbsp;declare -l var1 # Will change to lowercase
782 4&nbsp;var1=MixedCaseVARIABLE
783 5&nbsp;echo "$var1" # mixedcasevariable
784 6&nbsp;# Same effect as echo $var1 | tr A-Z a-z
785 7&nbsp;
786 8&nbsp;declare -c var2 # Changes only initial char to uppercase.
787 9&nbsp;var2=originally_lowercase
788 10&nbsp;echo "$var2" # Originally_lowercase
789 11&nbsp;# NOT the same effect as echo $var2 | tr a-z A-Z</PRE
790 ></TD
791 ></TR
792 ></TABLE
793 ></P
794 ></LI
795 ><LI
796 ><P
797 ><A
798 NAME="BRACEEXPREF4"
799 ></A
800 >
801 <A
802 HREF="special-chars.html#BRACEEXPREF"
803 >Brace expansion</A
804 > has more options.</P
805 ><P
806 ><I
807 CLASS="FIRSTTERM"
808 >Increment/decrement</I
809 >, specified in the
810 final term within braces.</P
811 ><P
812 ><TABLE
813 BORDER="0"
814 BGCOLOR="#E0E0E0"
815 WIDTH="90%"
816 ><TR
817 ><TD
818 ><PRE
819 CLASS="PROGRAMLISTING"
820 > 1&nbsp;#!/bin/bash4
821 2&nbsp;
822 3&nbsp;echo {40..60..2}
823 4&nbsp;# 40 42 44 46 48 50 52 54 56 58 60
824 5&nbsp;# All the even numbers, between 40 and 60.
825 6&nbsp;
826 7&nbsp;echo {60..40..2}
827 8&nbsp;# 60 58 56 54 52 50 48 46 44 42 40
828 9&nbsp;# All the even numbers, between 40 and 60, counting backwards.
829 10&nbsp;# In effect, a decrement.
830 11&nbsp;echo {60..40..-2}
831 12&nbsp;# The same output. The minus sign is not necessary.
832 13&nbsp;
833 14&nbsp;# But, what about letters and symbols?
834 15&nbsp;echo {X..d}
835 16&nbsp;# X Y Z [ ] ^ _ ` a b c d
836 17&nbsp;# Does not echo the \ which escapes a space.</PRE
837 ></TD
838 ></TR
839 ></TABLE
840 ></P
841 ><P
842 ><I
843 CLASS="FIRSTTERM"
844 >Zero-padding</I
845 >, specified in the
846 first term within braces, prefixes each term in the output
847 with the <SPAN
848 CLASS="emphasis"
849 ><I
850 CLASS="EMPHASIS"
851 >same number</I
852 ></SPAN
853 > of zeroes.</P
854 ><TABLE
855 BORDER="0"
856 BGCOLOR="#E0E0E0"
857 WIDTH="90%"
858 ><TR
859 ><TD
860 ><PRE
861 CLASS="SCREEN"
862 > <TT
863 CLASS="PROMPT"
864 >bash4$ </TT
865 ><TT
866 CLASS="USERINPUT"
867 ><B
868 >echo {010..15}</B
869 ></TT
870 >
871 <TT
872 CLASS="COMPUTEROUTPUT"
873 >010 011 012 013 014 015</TT
874 >
875
876
877 <TT
878 CLASS="PROMPT"
879 >bash4$ </TT
880 ><TT
881 CLASS="USERINPUT"
882 ><B
883 >echo {000..10}</B
884 ></TT
885 >
886 <TT
887 CLASS="COMPUTEROUTPUT"
888 >000 001 002 003 004 005 006 007 008 009 010</TT
889 >
890 </PRE
891 ></TD
892 ></TR
893 ></TABLE
894 ></LI
895 ><LI
896 ><P
897 ><A
898 NAME="SUBSTREXTREF4"
899 ></A
900 ></P
901 ><P
902 ><A
903 HREF="bashver4.html#SUBSTREXTREF4"
904 ><I
905 CLASS="FIRSTTERM"
906 >Substring
907 extraction</I
908 > on <I
909 CLASS="FIRSTTERM"
910 >positional
911 parameters</I
912 ></A
913 > now starts with <A
914 HREF="othertypesv.html#SCRNAMEPARAM"
915 >$0</A
916 > as the
917 <I
918 CLASS="FIRSTTERM"
919 >zero-index</I
920 >. (This corrects an
921 inconsistency in the treatment of positional parameters.)</P
922 ><P
923 ><TABLE
924 BORDER="0"
925 BGCOLOR="#E0E0E0"
926 WIDTH="90%"
927 ><TR
928 ><TD
929 ><PRE
930 CLASS="PROGRAMLISTING"
931 > 1&nbsp;#!/bin/bash
932 2&nbsp;# show-params.bash
933 3&nbsp;# Requires version 4+ of Bash.
934 4&nbsp;
935 5&nbsp;# Invoke this scripts with at least one positional parameter.
936 6&nbsp;
937 7&nbsp;E_BADPARAMS=99
938 8&nbsp;
939 9&nbsp;if [ -z "$1" ]
940 10&nbsp;then
941 11&nbsp; echo "Usage $0 param1 ..."
942 12&nbsp; exit $E_BADPARAMS
943 13&nbsp;fi
944 14&nbsp;
945 15&nbsp;echo ${@:0}
946 16&nbsp;
947 17&nbsp;# bash3 show-params.bash4 one two three
948 18&nbsp;# one two three
949 19&nbsp;
950 20&nbsp;# bash4 show-params.bash4 one two three
951 21&nbsp;# show-params.bash4 one two three
952 22&nbsp;
953 23&nbsp;# $0 $1 $2 $3</PRE
954 ></TD
955 ></TR
956 ></TABLE
957 ></P
958 ></LI
959 ><LI
960 ><P
961 ><A
962 NAME="GLOBSTARREF"
963 ></A
964 >The new <SPAN
965 CLASS="TOKEN"
966 >**</SPAN
967 >
968 <A
969 HREF="globbingref.html"
970 >globbing</A
971 > operator
972 matches filenames and directories
973 <A
974 HREF="localvar.html#RECURSIONREF0"
975 >recursively</A
976 >.</P
977 ><P
978 ><TABLE
979 BORDER="0"
980 BGCOLOR="#E0E0E0"
981 WIDTH="90%"
982 ><TR
983 ><TD
984 ><PRE
985 CLASS="PROGRAMLISTING"
986 > 1&nbsp;#!/bin/bash4
987 2&nbsp;# filelist.bash4
988 3&nbsp;
989 4&nbsp;shopt -s globstar # Must enable globstar, otherwise ** doesn't work.
990 5&nbsp; # The globstar shell option is new to version 4 of Bash.
991 6&nbsp;
992 7&nbsp;echo "Using *"; echo
993 8&nbsp;for filename in *
994 9&nbsp;do
995 10&nbsp; echo "$filename"
996 11&nbsp;done # Lists only files in current directory ($PWD).
997 12&nbsp;
998 13&nbsp;echo; echo "--------------"; echo
999 14&nbsp;
1000 15&nbsp;echo "Using **"
1001 16&nbsp;for filename in **
1002 17&nbsp;do
1003 18&nbsp; echo "$filename"
1004 19&nbsp;done # Lists complete file tree, recursively.
1005 20&nbsp;
1006 21&nbsp;exit
1007 22&nbsp;
1008 23&nbsp;Using *
1009 24&nbsp;
1010 25&nbsp;allmyfiles
1011 26&nbsp;filelist.bash4
1012 27&nbsp;
1013 28&nbsp;--------------
1014 29&nbsp;
1015 30&nbsp;Using **
1016 31&nbsp;
1017 32&nbsp;allmyfiles
1018 33&nbsp;allmyfiles/file.index.txt
1019 34&nbsp;allmyfiles/my_music
1020 35&nbsp;allmyfiles/my_music/me-singing-60s-folksongs.ogg
1021 36&nbsp;allmyfiles/my_music/me-singing-opera.ogg
1022 37&nbsp;allmyfiles/my_music/piano-lesson.1.ogg
1023 38&nbsp;allmyfiles/my_pictures
1024 39&nbsp;allmyfiles/my_pictures/at-beach-with-Jade.png
1025 40&nbsp;allmyfiles/my_pictures/picnic-with-Melissa.png
1026 41&nbsp;filelist.bash4</PRE
1027 ></TD
1028 ></TR
1029 ></TABLE
1030 ></P
1031 ></LI
1032 ><LI
1033 ><P
1034 >The new <A
1035 HREF="variables2.html#BASHPIDREF"
1036 >$BASHPID</A
1037 >
1038 internal variable.</P
1039 ></LI
1040 ><LI
1041 ><P
1042 ><A
1043 NAME="CNFH"
1044 ></A
1045 ></P
1046 ><P
1047 >There is a new <A
1048 HREF="internal.html#BUILTINREF"
1049 >builtin</A
1050 >
1051 error-handling function named
1052 <B
1053 CLASS="COMMAND"
1054 >command_not_found_handle</B
1055 >.</P
1056 ><P
1057 ><TABLE
1058 BORDER="0"
1059 BGCOLOR="#E0E0E0"
1060 WIDTH="90%"
1061 ><TR
1062 ><TD
1063 ><PRE
1064 CLASS="PROGRAMLISTING"
1065 > 1&nbsp;#!/bin/bash4
1066 2&nbsp;
1067 3&nbsp;command_not_found_handle ()
1068 4&nbsp;{ # Accepts implicit parameters.
1069 5&nbsp; echo "The following command is not valid: \""$1\"""
1070 6&nbsp; echo "With the following argument(s): \""$2\"" \""$3\""" # $4, $5 ...
1071 7&nbsp;} # $1, $2, etc. are not explicitly passed to the function.
1072 8&nbsp;
1073 9&nbsp;bad_command arg1 arg2
1074 10&nbsp;
1075 11&nbsp;# The following command is not valid: "bad_command"
1076 12&nbsp;# With the following argument(s): "arg1" "arg2"</PRE
1077 ></TD
1078 ></TR
1079 ></TABLE
1080 ></P
1081 ></LI
1082 ></UL
1083 ><TABLE
1084 CLASS="SIDEBAR"
1085 BORDER="1"
1086 CELLPADDING="5"
1087 ><TR
1088 ><TD
1089 ><DIV
1090 CLASS="SIDEBAR"
1091 ><A
1092 NAME="AEN21170"
1093 ></A
1094 ><P
1095 ><SPAN
1096 CLASS="emphasis"
1097 ><I
1098 CLASS="EMPHASIS"
1099 >Editorial comment</I
1100 ></SPAN
1101 ></P
1102 ><P
1103 >Associative arrays? Coprocesses? Whatever happened
1104 to the lean and mean Bash we have come to know and love?
1105 Could it be suffering from (horrors!) <SPAN
1106 CLASS="QUOTE"
1107 >"feature
1108 creep"</SPAN
1109 >? Or perhaps even Korn shell envy?</P
1110 ><P
1111 ><SPAN
1112 CLASS="emphasis"
1113 ><I
1114 CLASS="EMPHASIS"
1115 >Note to Chet Ramey:</I
1116 ></SPAN
1117 > Please add only
1118 <SPAN
1119 CLASS="emphasis"
1120 ><I
1121 CLASS="EMPHASIS"
1122 >essential</I
1123 ></SPAN
1124 > features in future Bash
1125 releases -- perhaps <I
1126 CLASS="FIRSTTERM"
1127 >for-each</I
1128 >
1129 loops and support for multi-dimensional arrays.
1130 <A
1131 NAME="AEN21179"
1132 HREF="#FTN.AEN21179"
1133 >[5]</A
1134 >
1135 Most Bash users won't need, won't use, and likely won't greatly
1136 appreciate complex <SPAN
1137 CLASS="QUOTE"
1138 >"features"</SPAN
1139 > like built-in
1140 debuggers, Perl interfaces, and bolt-on rocket boosters.</P
1141 ></DIV
1142 ></TD
1143 ></TR
1144 ></TABLE
1145 ><DIV
1146 CLASS="SECT2"
1147 ><H2
1148 CLASS="SECT2"
1149 ><A
1150 NAME="AEN21183"
1151 ></A
1152 >37.3.1. Bash, version 4.1</H2
1153 ><P
1154 ><A
1155 NAME="BASH41"
1156 ></A
1157 >Version 4.1 of Bash, released in May,
1158 2010, was primarily a bugfix update.</P
1159 ><UL
1160 ><LI
1161 ><P
1162 >The <A
1163 HREF="internal.html#PRINTFREF"
1164 >printf</A
1165 > command
1166 now accepts a <TT
1167 CLASS="OPTION"
1168 >-v</TT
1169 > option for setting <A
1170 HREF="arrays.html#ARRAYREF"
1171 >array</A
1172 > indices.</P
1173 ></LI
1174 ><LI
1175 ><P
1176 >Within <A
1177 HREF="tests.html#DBLBRACKETS"
1178 >double brackets</A
1179 >,
1180 the <B
1181 CLASS="COMMAND"
1182 >&#62;</B
1183 > and <B
1184 CLASS="COMMAND"
1185 >&#60;</B
1186 >
1187 string comparison operators now conform to the <A
1188 HREF="localization.html#LOCALEREF"
1189 >locale</A
1190 >. Since the locale setting may
1191 affect the sorting order of string expressions, this
1192 has side-effects on comparison tests within
1193 <SPAN
1194 CLASS="emphasis"
1195 ><I
1196 CLASS="EMPHASIS"
1197 >[[ ... ]]</I
1198 ></SPAN
1199 > expressions.</P
1200 ></LI
1201 ><LI
1202 ><P
1203 >The <A
1204 HREF="internal.html#READREF"
1205 >read</A
1206 > builtin
1207 now takes a <TT
1208 CLASS="OPTION"
1209 >-N</TT
1210 > option (<I
1211 CLASS="FIRSTTERM"
1212 >read -N
1213 chars</I
1214 >), which causes the <I
1215 CLASS="FIRSTTERM"
1216 >read</I
1217 >
1218 to terminate after <I
1219 CLASS="FIRSTTERM"
1220 >chars</I
1221 >
1222 characters.</P
1223 ><DIV
1224 CLASS="EXAMPLE"
1225 ><HR><A
1226 NAME="READN"
1227 ></A
1228 ><P
1229 ><B
1230 >Example 37-8. Reading N characters</B
1231 ></P
1232 ><TABLE
1233 BORDER="0"
1234 BGCOLOR="#E0E0E0"
1235 WIDTH="90%"
1236 ><TR
1237 ><TD
1238 ><PRE
1239 CLASS="PROGRAMLISTING"
1240 > 1&nbsp;#!/bin/bash
1241 2&nbsp;# Requires Bash version -ge 4.1 ...
1242 3&nbsp;
1243 4&nbsp;num_chars=61
1244 5&nbsp;
1245 6&nbsp;read -N $num_chars var &#60; $0 # Read first 61 characters of script!
1246 7&nbsp;echo "$var"
1247 8&nbsp;exit
1248 9&nbsp;
1249 10&nbsp;####### Output of Script #######
1250 11&nbsp;
1251 12&nbsp;#!/bin/bash
1252 13&nbsp;# Requires Bash version -ge 4.1 ...
1253 14&nbsp;
1254 15&nbsp;num_chars=61</PRE
1255 ></TD
1256 ></TR
1257 ></TABLE
1258 ><HR></DIV
1259 ></LI
1260 ><LI
1261 ><P
1262 ><A
1263 HREF="here-docs.html#HEREDOCREF"
1264 >Here documents</A
1265 >
1266 embedded in <A
1267 HREF="varassignment.html#COMMANDSUBREF0"
1268 > <TT
1269 CLASS="USERINPUT"
1270 ><B
1271 >$( ... )</B
1272 ></TT
1273 > command substitution</A
1274 >
1275 constructs may terminate with a simple
1276 <B
1277 CLASS="COMMAND"
1278 >)</B
1279 >.</P
1280 ><DIV
1281 CLASS="EXAMPLE"
1282 ><HR><A
1283 NAME="HERECOMMSUB"
1284 ></A
1285 ><P
1286 ><B
1287 >Example 37-9. Using a <I
1288 CLASS="FIRSTTERM"
1289 >here document</I
1290 >
1291 to set a variable</B
1292 ></P
1293 ><TABLE
1294 BORDER="0"
1295 BGCOLOR="#E0E0E0"
1296 WIDTH="90%"
1297 ><TR
1298 ><TD
1299 ><PRE
1300 CLASS="PROGRAMLISTING"
1301 > 1&nbsp;#!/bin/bash
1302 2&nbsp;# here-commsub.sh
1303 3&nbsp;# Requires Bash version -ge 4.1 ...
1304 4&nbsp;
1305 5&nbsp;multi_line_var=$( cat &#60;&#60;ENDxxx
1306 6&nbsp;------------------------------
1307 7&nbsp;This is line 1 of the variable
1308 8&nbsp;This is line 2 of the variable
1309 9&nbsp;This is line 3 of the variable
1310 10&nbsp;------------------------------
1311 11&nbsp;ENDxxx)
1312 12&nbsp;
1313 13&nbsp;# Rather than what Bash 4.0 requires:
1314 14&nbsp;#+ that the terminating limit string and
1315 15&nbsp;#+ the terminating close-parenthesis be on separate lines.
1316 16&nbsp;
1317 17&nbsp;# ENDxxx
1318 18&nbsp;# )
1319 19&nbsp;
1320 20&nbsp;
1321 21&nbsp;echo "$multi_line_var"
1322 22&nbsp;
1323 23&nbsp;# Bash still emits a warning, though.
1324 24&nbsp;# warning: here-document at line 10 delimited
1325 25&nbsp;#+ by end-of-file (wanted `ENDxxx')</PRE
1326 ></TD
1327 ></TR
1328 ></TABLE
1329 ><HR></DIV
1330 ></LI
1331 ></UL
1332 ></DIV
1333 ><DIV
1334 CLASS="SECT2"
1335 ><H2
1336 CLASS="SECT2"
1337 ><A
1338 NAME="AEN21220"
1339 ></A
1340 >37.3.2. Bash, version 4.2</H2
1341 ><P
1342 ><A
1343 NAME="BASH42"
1344 ></A
1345 >Version 4.2 of Bash, released
1346 in February, 2011, contains a number of new features and
1347 enhancements, in addition to bugfixes.</P
1348 ><UL
1349 ><LI
1350 ><P
1351 >Bash now supports the the
1352 <TT
1353 CLASS="REPLACEABLE"
1354 ><I
1355 >\u</I
1356 ></TT
1357 >
1358 and <TT
1359 CLASS="REPLACEABLE"
1360 ><I
1361 >\U</I
1362 ></TT
1363 >
1364 <I
1365 CLASS="FIRSTTERM"
1366 >Unicode</I
1367 > escape.</P
1368 ><P
1369 ><A
1370 NAME="UNICODEREF"
1371 ></A
1372 ></P
1373 ><TABLE
1374 CLASS="SIDEBAR"
1375 BORDER="1"
1376 CELLPADDING="5"
1377 ><TR
1378 ><TD
1379 ><DIV
1380 CLASS="SIDEBAR"
1381 ><A
1382 NAME="AEN21232"
1383 ></A
1384 ><P
1385 >Unicode is a cross-platform standard for encoding
1386 into numerical values letters and graphic symbols.
1387 This permits representing and displaying characters
1388 in foreign alphabets and unusual fonts.</P
1389 ></DIV
1390 ></TD
1391 ></TR
1392 ></TABLE
1393 ><P
1394 ><A
1395 NAME="UNICODEREF2"
1396 ></A
1397 ></P
1398 ><P
1399 ><TABLE
1400 BORDER="0"
1401 BGCOLOR="#E0E0E0"
1402 WIDTH="90%"
1403 ><TR
1404 ><TD
1405 ><PRE
1406 CLASS="PROGRAMLISTING"
1407 > 1&nbsp;echo -e '\u2630' # Horizontal triple bar character.
1408 2&nbsp;# Equivalent to the more roundabout:
1409 3&nbsp;echo -e "\xE2\x98\xB0"
1410 4&nbsp; # Recognized by earlier Bash versions.
1411 5&nbsp;
1412 6&nbsp;echo -e '\u220F' # PI (Greek letter and mathematical symbol)
1413 7&nbsp;echo -e '\u0416' # Capital "ZHE" (Cyrillic letter)
1414 8&nbsp;echo -e '\u2708' # Airplane (Dingbat font) symbol
1415 9&nbsp;echo -e '\u2622' # Radioactivity trefoil
1416 10&nbsp;
1417 11&nbsp;echo -e "The amplifier circuit requires a 100 \u2126 pull-up resistor."
1418 12&nbsp;
1419 13&nbsp;
1420 14&nbsp;unicode_var='\u2640'
1421 15&nbsp;echo -e $unicode_var # Female symbol
1422 16&nbsp;printf "$unicode_var \n" # Female symbol, with newline
1423 17&nbsp;
1424 18&nbsp;
1425 19&nbsp;# And for something a bit more elaborate . . .
1426 20&nbsp;
1427 21&nbsp;# We can store Unicode symbols in an associative array,
1428 22&nbsp;#+ then retrieve them by name.
1429 23&nbsp;# Run this in a gnome-terminal or a terminal with a large, bold font
1430 24&nbsp;#+ for better legibility.
1431 25&nbsp;
1432 26&nbsp;declare -A symbol # Associative array.
1433 27&nbsp;
1434 28&nbsp;symbol[script_E]='\u2130'
1435 29&nbsp;symbol[script_F]='\u2131'
1436 30&nbsp;symbol[script_J]='\u2110'
1437 31&nbsp;symbol[script_M]='\u2133'
1438 32&nbsp;symbol[Rx]='\u211E'
1439 33&nbsp;symbol[TEL]='\u2121'
1440 34&nbsp;symbol[FAX]='\u213B'
1441 35&nbsp;symbol[care_of]='\u2105'
1442 36&nbsp;symbol[account]='\u2100'
1443 37&nbsp;symbol[trademark]='\u2122'
1444 38&nbsp;
1445 39&nbsp;
1446 40&nbsp;echo -ne "${symbol[script_E]} "
1447 41&nbsp;echo -ne "${symbol[script_F]} "
1448 42&nbsp;echo -ne "${symbol[script_J]} "
1449 43&nbsp;echo -ne "${symbol[script_M]} "
1450 44&nbsp;echo -ne "${symbol[Rx]} "
1451 45&nbsp;echo -ne "${symbol[TEL]} "
1452 46&nbsp;echo -ne "${symbol[FAX]} "
1453 47&nbsp;echo -ne "${symbol[care_of]} "
1454 48&nbsp;echo -ne "${symbol[account]} "
1455 49&nbsp;echo -ne "${symbol[trademark]} "
1456 50&nbsp;echo</PRE
1457 ></TD
1458 ></TR
1459 ></TABLE
1460 ></P
1461 ><DIV
1462 CLASS="NOTE"
1463 ><TABLE
1464 CLASS="NOTE"
1465 WIDTH="90%"
1466 BORDER="0"
1467 ><TR
1468 ><TD
1469 WIDTH="25"
1470 ALIGN="CENTER"
1471 VALIGN="TOP"
1472 ><IMG
1473 SRC="common/note.png"
1474 HSPACE="5"
1475 ALT="Note"></TD
1476 ><TD
1477 ALIGN="LEFT"
1478 VALIGN="TOP"
1479 ><P
1480 >The above example uses the
1481 <A
1482 HREF="escapingsection.html#STRQ"
1483 ><B
1484 CLASS="COMMAND"
1485 >$' ... '</B
1486 ></A
1487 >
1488 <I
1489 CLASS="FIRSTTERM"
1490 >string-expansion</I
1491 > construct.</P
1492 ></TD
1493 ></TR
1494 ></TABLE
1495 ></DIV
1496 ></LI
1497 ><LI
1498 ><P
1499 ><A
1500 NAME="LASTPIPEREF"
1501 ></A
1502 ></P
1503 ><P
1504 >When the <TT
1505 CLASS="REPLACEABLE"
1506 ><I
1507 >lastpipe</I
1508 ></TT
1509 > shell option
1510 is set, the last command in a <A
1511 HREF="special-chars.html#PIPEREF"
1512 >pipe</A
1513 > <SPAN
1514 CLASS="emphasis"
1515 ><I
1516 CLASS="EMPHASIS"
1517 >doesn't run in a
1518 subshell</I
1519 ></SPAN
1520 >.</P
1521 ><DIV
1522 CLASS="EXAMPLE"
1523 ><HR><A
1524 NAME="LASTPIPEOPT"
1525 ></A
1526 ><P
1527 ><B
1528 >Example 37-10. Piping input to a <A
1529 HREF="internal.html#READREF"
1530 >read</A
1531 ></B
1532 ></P
1533 ><TABLE
1534 BORDER="0"
1535 BGCOLOR="#E0E0E0"
1536 WIDTH="90%"
1537 ><TR
1538 ><TD
1539 ><PRE
1540 CLASS="PROGRAMLISTING"
1541 > 1&nbsp;#!/bin/bash
1542 2&nbsp;# lastpipe-option.sh
1543 3&nbsp;
1544 4&nbsp;line='' # Null value.
1545 5&nbsp;echo "\$line = "$line"" # $line =
1546 6&nbsp;
1547 7&nbsp;echo
1548 8&nbsp;
1549 9&nbsp;shopt -s lastpipe # Error on Bash version -lt 4.2.
1550 10&nbsp;echo "Exit status of attempting to set \"lastpipe\" option is $?"
1551 11&nbsp;# 1 if Bash version -lt 4.2, 0 otherwise.
1552 12&nbsp;
1553 13&nbsp;echo
1554 14&nbsp;
1555 15&nbsp;head -1 $0 | read line # Pipe the first line of the script to read.
1556 16&nbsp;# ^^^^^^^^^ Not in a subshell!!!
1557 17&nbsp;
1558 18&nbsp;echo "\$line = "$line""
1559 19&nbsp;# Older Bash releases $line =
1560 20&nbsp;# Bash version 4.2 $line = #!/bin/bash</PRE
1561 ></TD
1562 ></TR
1563 ></TABLE
1564 ><HR></DIV
1565 ><P
1566 >This option offers possible <SPAN
1567 CLASS="QUOTE"
1568 >"fixups"</SPAN
1569 >
1570 for these example scripts:
1571 <A
1572 HREF="gotchas.html#BADREAD"
1573 >Example 34-3</A
1574 > and
1575 <A
1576 HREF="internal.html#READPIPE"
1577 >Example 15-8</A
1578 >.</P
1579 ></LI
1580 ><LI
1581 ><P
1582 >Negative <A
1583 HREF="arrays.html#ARRAYREF"
1584 >array</A
1585 > indices
1586 permit counting backwards from the end of an array.</P
1587 ><DIV
1588 CLASS="EXAMPLE"
1589 ><HR><A
1590 NAME="NEGARRAY"
1591 ></A
1592 ><P
1593 ><B
1594 >Example 37-11. Negative array indices</B
1595 ></P
1596 ><TABLE
1597 BORDER="0"
1598 BGCOLOR="#E0E0E0"
1599 WIDTH="90%"
1600 ><TR
1601 ><TD
1602 ><PRE
1603 CLASS="PROGRAMLISTING"
1604 > 1&nbsp;#!/bin/bash
1605 2&nbsp;# neg-array.sh
1606 3&nbsp;# Requires Bash, version -ge 4.2.
1607 4&nbsp;
1608 5&nbsp;array=( zero one two three four five ) # Six-element array.
1609 6&nbsp;# 0 1 2 3 4 5
1610 7&nbsp;# -6 -5 -4 -3 -2 -1
1611 8&nbsp;
1612 9&nbsp;# Negative array indices now permitted.
1613 10&nbsp;echo ${array[-1]} # five
1614 11&nbsp;echo ${array[-2]} # four
1615 12&nbsp;# ...
1616 13&nbsp;echo ${array[-6]} # zero
1617 14&nbsp;# Negative array indices count backward from the last element+1.
1618 15&nbsp;
1619 16&nbsp;# But, you cannot index past the beginning of the array.
1620 17&nbsp;echo ${array[-7]} # array: bad array subscript
1621 18&nbsp;
1622 19&nbsp;
1623 20&nbsp;# So, what is this new feature good for?
1624 21&nbsp;
1625 22&nbsp;echo "The last element in the array is "${array[-1]}""
1626 23&nbsp;# Which is quite a bit more straightforward than:
1627 24&nbsp;echo "The last element in the array is "${array[${#array[*]}-1]}""
1628 25&nbsp;echo
1629 26&nbsp;
1630 27&nbsp;# And ...
1631 28&nbsp;
1632 29&nbsp;index=0
1633 30&nbsp;let "neg_element_count = 0 - ${#array[*]}"
1634 31&nbsp;# Number of elements, converted to a negative number.
1635 32&nbsp;
1636 33&nbsp;while [ $index -gt $neg_element_count ]; do
1637 34&nbsp; ((index--)); echo -n "${array[index]} "
1638 35&nbsp;done # Lists the elements in the array, backwards.
1639 36&nbsp; # We have just simulated the "tac" command on this array.
1640 37&nbsp;
1641 38&nbsp;echo
1642 39&nbsp;
1643 40&nbsp;# See also neg-offset.sh.</PRE
1644 ></TD
1645 ></TR
1646 ></TABLE
1647 ><HR></DIV
1648 ></LI
1649 ><LI
1650 ><P
1651 ><A
1652 HREF="manipulatingvars.html#SUBSTREXTR01"
1653 >Substring extraction</A
1654 >
1655 uses a negative <I
1656 CLASS="FIRSTTERM"
1657 >length</I
1658 > parameter to
1659 specify an offset from the <I
1660 CLASS="FIRSTTERM"
1661 >end</I
1662 > of the
1663 target string.</P
1664 ><DIV
1665 CLASS="EXAMPLE"
1666 ><HR><A
1667 NAME="NEGOFFSET"
1668 ></A
1669 ><P
1670 ><B
1671 >Example 37-12. Negative parameter in string-extraction
1672 construct</B
1673 ></P
1674 ><TABLE
1675 BORDER="0"
1676 BGCOLOR="#E0E0E0"
1677 WIDTH="90%"
1678 ><TR
1679 ><TD
1680 ><PRE
1681 CLASS="PROGRAMLISTING"
1682 > 1&nbsp;#!/bin/bash
1683 2&nbsp;# Bash, version -ge 4.2
1684 3&nbsp;# Negative length-index in substring extraction.
1685 4&nbsp;# Important: It changes the interpretation of this construct!
1686 5&nbsp;
1687 6&nbsp;stringZ=abcABC123ABCabc
1688 7&nbsp;
1689 8&nbsp;echo ${stringZ} # abcABC123ABCabc
1690 9&nbsp;# Position within string: 0123456789.....
1691 10&nbsp;echo ${stringZ:2:3} # cAB
1692 11&nbsp;# Count 2 chars forward from string beginning, and extract 3 chars.
1693 12&nbsp;# ${string:position:length}
1694 13&nbsp;
1695 14&nbsp;# So far, nothing new, but now ...
1696 15&nbsp;
1697 16&nbsp; # abcABC123ABCabc
1698 17&nbsp;# Position within string: 0123....6543210
1699 18&nbsp;echo ${stringZ:3:-6} # ABC123
1700 19&nbsp;# ^
1701 20&nbsp;# Index 3 chars forward from beginning and 6 chars backward from end,
1702 21&nbsp;#+ and extract everything in between.
1703 22&nbsp;# ${string:offset-from-front:offset-from-end}
1704 23&nbsp;# When the "length" parameter is negative,
1705 24&nbsp;#+ it serves as an offset-from-end parameter.
1706 25&nbsp;
1707 26&nbsp;# See also neg-array.sh.</PRE
1708 ></TD
1709 ></TR
1710 ></TABLE
1711 ><HR></DIV
1712 ></LI
1713 ></UL
1714 ></DIV
1715 ></DIV
1716 ><H3
1717 CLASS="FOOTNOTES"
1718 >Notes</H3
1719 ><TABLE
1720 BORDER="0"
1721 CLASS="FOOTNOTES"
1722 WIDTH="100%"
1723 ><TR
1724 ><TD
1725 ALIGN="LEFT"
1726 VALIGN="TOP"
1727 WIDTH="5%"
1728 ><A
1729 NAME="FTN.AEN21025"
1730 HREF="bashver4.html#AEN21025"
1731 >[1]</A
1732 ></TD
1733 ><TD
1734 ALIGN="LEFT"
1735 VALIGN="TOP"
1736 WIDTH="95%"
1737 ><P
1738 >To be more specific, Bash 4+ has
1739 <SPAN
1740 CLASS="emphasis"
1741 ><I
1742 CLASS="EMPHASIS"
1743 >limited</I
1744 ></SPAN
1745 > support for associative
1746 arrays. It's a bare-bones implementation,
1747 and it lacks the much of the functionality of such
1748 arrays in other programming languages. Note, however,
1749 that <A
1750 HREF="optimizations.html#ASSOCARRTST"
1751 >associative arrays in
1752 Bash seem to execute faster and more efficiently than
1753 numerically-indexed arrays</A
1754 >.</P
1755 ></TD
1756 ></TR
1757 ><TR
1758 ><TD
1759 ALIGN="LEFT"
1760 VALIGN="TOP"
1761 WIDTH="5%"
1762 ><A
1763 NAME="FTN.AEN21068"
1764 HREF="bashver4.html#AEN21068"
1765 >[2]</A
1766 ></TD
1767 ><TD
1768 ALIGN="LEFT"
1769 VALIGN="TOP"
1770 WIDTH="95%"
1771 ><P
1772 >Copyright 1995-2009 by Chester Ramey.</P
1773 ></TD
1774 ></TR
1775 ><TR
1776 ><TD
1777 ALIGN="LEFT"
1778 VALIGN="TOP"
1779 WIDTH="5%"
1780 ><A
1781 NAME="FTN.AEN21096"
1782 HREF="bashver4.html#AEN21096"
1783 >[3]</A
1784 ></TD
1785 ><TD
1786 ALIGN="LEFT"
1787 VALIGN="TOP"
1788 WIDTH="95%"
1789 ><P
1790 >This only works with <A
1791 HREF="special-chars.html#PIPEREF"
1792 >pipes</A
1793 > and certain other
1794 <I
1795 CLASS="FIRSTTERM"
1796 >special</I
1797 > files.</P
1798 ></TD
1799 ></TR
1800 ><TR
1801 ><TD
1802 ALIGN="LEFT"
1803 VALIGN="TOP"
1804 WIDTH="5%"
1805 ><A
1806 NAME="FTN.AEN21101"
1807 HREF="bashver4.html#AEN21101"
1808 >[4]</A
1809 ></TD
1810 ><TD
1811 ALIGN="LEFT"
1812 VALIGN="TOP"
1813 WIDTH="95%"
1814 ><P
1815 >But only in conjunction with
1816 <A
1817 HREF="internal.html#READLINEREF"
1818 >readline</A
1819 >, i.e.,
1820 from the command-line.</P
1821 ></TD
1822 ></TR
1823 ><TR
1824 ><TD
1825 ALIGN="LEFT"
1826 VALIGN="TOP"
1827 WIDTH="5%"
1828 ><A
1829 NAME="FTN.AEN21179"
1830 HREF="bashver4.html#AEN21179"
1831 >[5]</A
1832 ></TD
1833 ><TD
1834 ALIGN="LEFT"
1835 VALIGN="TOP"
1836 WIDTH="95%"
1837 ><P
1838 >And while you're at it, consider fixing
1839 the notorious <A
1840 HREF="internal.html#PIPEREADREF0"
1841 >piped read</A
1842 >
1843 problem.</P
1844 ></TD
1845 ></TR
1846 ></TABLE
1847 ><DIV
1848 CLASS="NAVFOOTER"
1849 ><HR
1850 ALIGN="LEFT"
1851 WIDTH="100%"><TABLE
1852 SUMMARY="Footer navigation table"
1853 WIDTH="100%"
1854 BORDER="0"
1855 CELLPADDING="0"
1856 CELLSPACING="0"
1857 ><TR
1858 ><TD
1859 WIDTH="33%"
1860 ALIGN="left"
1861 VALIGN="top"
1862 ><A
1863 HREF="bashver3.html"
1864 ACCESSKEY="P"
1865 >Prev</A
1866 ></TD
1867 ><TD
1868 WIDTH="34%"
1869 ALIGN="center"
1870 VALIGN="top"
1871 ><A
1872 HREF="index.html"
1873 ACCESSKEY="H"
1874 >Home</A
1875 ></TD
1876 ><TD
1877 WIDTH="33%"
1878 ALIGN="right"
1879 VALIGN="top"
1880 ><A
1881 HREF="endnotes.html"
1882 ACCESSKEY="N"
1883 >Next</A
1884 ></TD
1885 ></TR
1886 ><TR
1887 ><TD
1888 WIDTH="33%"
1889 ALIGN="left"
1890 VALIGN="top"
1891 >Bash, version 3</TD
1892 ><TD
1893 WIDTH="34%"
1894 ALIGN="center"
1895 VALIGN="top"
1896 ><A
1897 HREF="bash2.html"
1898 ACCESSKEY="U"
1899 >Up</A
1900 ></TD
1901 ><TD
1902 WIDTH="33%"
1903 ALIGN="right"
1904 VALIGN="top"
1905 >Endnotes</TD
1906 ></TR
1907 ></TABLE
1908 ></DIV
1909 ></BODY
1910 ></HTML
1911 >