Debian Codegraph @

HTML/parameter-substitution.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Parameter Substitution</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="Manipulating Variables"
15 HREF="manipulatingvars.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Manipulating Variables"
18 HREF="manipulatingvars.html"><LINK
19 REL="NEXT"
20 TITLE="Loops and Branches"
21 HREF="loops.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="manipulatingvars.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 10. Manipulating Variables</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="loops.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="PARAMETER-SUBSTITUTION"
96 ></A
97 >10.2. Parameter Substitution</H1
98 ><P
99 ><A
100 NAME="PARAMSUBREF"
101 ></A
102 ></P
103 ><DIV
104 CLASS="VARIABLELIST"
105 ><P
106 ><B
107 ><A
108 NAME="PSSUB1"
109 ></A
110 >Manipulating and/or expanding variables</B
111 ></P
112 ><DL
113 ><DT
114 ><TT
115 CLASS="USERINPUT"
116 ><B
117 >${parameter}</B
118 ></TT
119 ></DT
120 ><DD
121 ><P
122 >Same as <TT
123 CLASS="REPLACEABLE"
124 ><I
125 >$parameter</I
126 ></TT
127 >, i.e.,
128 value of the variable
129 <TT
130 CLASS="REPLACEABLE"
131 ><I
132 >parameter</I
133 ></TT
134 >.
135 In certain contexts, only the less ambiguous
136 <TT
137 CLASS="REPLACEABLE"
138 ><I
139 >${parameter}</I
140 ></TT
141 > form
142 works.</P
143 ><P
144 >May be used for concatenating variables with strings.</P
145 ><P
146 ><TABLE
147 BORDER="0"
148 BGCOLOR="#E0E0E0"
149 WIDTH="90%"
150 ><TR
151 ><TD
152 ><PRE
153 CLASS="PROGRAMLISTING"
154 > 1&nbsp;your_id=${USER}-on-${HOSTNAME}
155 2&nbsp;echo "$your_id"
156 3&nbsp;#
157 4&nbsp;echo "Old \$PATH = $PATH"
158 5&nbsp;PATH=${PATH}:/opt/bin # Add /opt/bin to $PATH for duration of script.
159 6&nbsp;echo "New \$PATH = $PATH"</PRE
160 ></TD
161 ></TR
162 ></TABLE
163 ></P
164 ></DD
165 ><DT
166 ><A
167 NAME="DEFPARAM1"
168 ></A
169 ><TT
170 CLASS="USERINPUT"
171 ><B
172 >${parameter-default}</B
173 ></TT
174 >, <TT
175 CLASS="USERINPUT"
176 ><B
177 >${parameter:-default}</B
178 ></TT
179 ></DT
180 ><DD
181 ><P
182 >If parameter not set, use default.</P
183 ><P
184 ><TABLE
185 BORDER="0"
186 BGCOLOR="#E0E0E0"
187 WIDTH="90%"
188 ><TR
189 ><TD
190 ><PRE
191 CLASS="PROGRAMLISTING"
192 > 1&nbsp;var1=1
193 2&nbsp;var2=2
194 3&nbsp;# var3 is unset.
195 4&nbsp;
196 5&nbsp;echo ${var1-$var2} # 1
197 6&nbsp;echo ${var3-$var2} # 2
198 7&nbsp;# ^ Note the $ prefix.
199 8&nbsp;
200 9&nbsp;
201 10&nbsp;
202 11&nbsp;echo ${username-`whoami`}
203 12&nbsp;# Echoes the result of `whoami`, if variable $username is still unset.</PRE
204 ></TD
205 ></TR
206 ></TABLE
207 ></P
208 ><DIV
209 CLASS="NOTE"
210 ><TABLE
211 CLASS="NOTE"
212 WIDTH="90%"
213 BORDER="0"
214 ><TR
215 ><TD
216 WIDTH="25"
217 ALIGN="CENTER"
218 VALIGN="TOP"
219 ><IMG
220 SRC="common/note.png"
221 HSPACE="5"
222 ALT="Note"></TD
223 ><TD
224 ALIGN="LEFT"
225 VALIGN="TOP"
226 ><P
227 ><A
228 NAME="UNDDR"
229 ></A
230 ><TT
231 CLASS="REPLACEABLE"
232 ><I
233 >${parameter-default}</I
234 ></TT
235 >
236 and <TT
237 CLASS="REPLACEABLE"
238 ><I
239 >${parameter:-default}</I
240 ></TT
241 >
242 are almost equivalent. The extra <SPAN
243 CLASS="TOKEN"
244 >:</SPAN
245 > makes
246 a difference only when <TT
247 CLASS="PARAMETER"
248 ><I
249 >parameter</I
250 ></TT
251 >
252 has been declared, but is null. </P
253 ></TD
254 ></TR
255 ></TABLE
256 ></DIV
257 ><P
258 ><TABLE
259 BORDER="0"
260 BGCOLOR="#E0E0E0"
261 WIDTH="90%"
262 ><TR
263 ><TD
264 ><PRE
265 CLASS="PROGRAMLISTING"
266 > 1&nbsp;#!/bin/bash
267 2&nbsp;# param-sub.sh
268 3&nbsp;
269 4&nbsp;# Whether a variable has been declared
270 5&nbsp;#+ affects triggering of the default option
271 6&nbsp;#+ even if the variable is null.
272 7&nbsp;
273 8&nbsp;username0=
274 9&nbsp;echo "username0 has been declared, but is set to null."
275 10&nbsp;echo "username0 = ${username0-`whoami`}"
276 11&nbsp;# Will not echo.
277 12&nbsp;
278 13&nbsp;echo
279 14&nbsp;
280 15&nbsp;echo username1 has not been declared.
281 16&nbsp;echo "username1 = ${username1-`whoami`}"
282 17&nbsp;# Will echo.
283 18&nbsp;
284 19&nbsp;username2=
285 20&nbsp;echo "username2 has been declared, but is set to null."
286 21&nbsp;echo "username2 = ${username2:-`whoami`}"
287 22&nbsp;# ^
288 23&nbsp;# Will echo because of :- rather than just - in condition test.
289 24&nbsp;# Compare to first instance, above.
290 25&nbsp;
291 26&nbsp;
292 27&nbsp;#
293 28&nbsp;
294 29&nbsp;# Once again:
295 30&nbsp;
296 31&nbsp;variable=
297 32&nbsp;# variable has been declared, but is set to null.
298 33&nbsp;
299 34&nbsp;echo "${variable-0}" # (no output)
300 35&nbsp;echo "${variable:-1}" # 1
301 36&nbsp;# ^
302 37&nbsp;
303 38&nbsp;unset variable
304 39&nbsp;
305 40&nbsp;echo "${variable-2}" # 2
306 41&nbsp;echo "${variable:-3}" # 3
307 42&nbsp;
308 43&nbsp;exit 0</PRE
309 ></TD
310 ></TR
311 ></TABLE
312 ></P
313 ><P
314 >The <I
315 CLASS="FIRSTTERM"
316 >default parameter</I
317 > construct
318 finds use in providing <SPAN
319 CLASS="QUOTE"
320 >"missing"</SPAN
321 > command-line
322 arguments in scripts.</P
323 ><P
324 > <TABLE
325 BORDER="0"
326 BGCOLOR="#E0E0E0"
327 WIDTH="90%"
328 ><TR
329 ><TD
330 ><PRE
331 CLASS="PROGRAMLISTING"
332 > 1&nbsp;DEFAULT_FILENAME=generic.data
333 2&nbsp;filename=${1:-$DEFAULT_FILENAME}
334 3&nbsp;# If not otherwise specified, the following command block operates
335 4&nbsp;#+ on the file "generic.data".
336 5&nbsp;# Begin-Command-Block
337 6&nbsp;# ...
338 7&nbsp;# ...
339 8&nbsp;# ...
340 9&nbsp;# End-Command-Block
341 10&nbsp;
342 11&nbsp;
343 12&nbsp;
344 13&nbsp;# From "hanoi2.bash" example:
345 14&nbsp;DISKS=${1:-E_NOPARAM} # Must specify how many disks.
346 15&nbsp;# Set $DISKS to $1 command-line-parameter,
347 16&nbsp;#+ or to $E_NOPARAM if that is unset.</PRE
348 ></TD
349 ></TR
350 ></TABLE
351 >
352 </P
353 ><P
354 >See also <A
355 HREF="special-chars.html#EX58"
356 >Example 3-4</A
357 >, <A
358 HREF="zeros.html#EX73"
359 >Example 31-2</A
360 >, and <A
361 HREF="contributed-scripts.html#COLLATZ"
362 >Example A-6</A
363 >.</P
364 ><P
365 >Compare this method with <A
366 HREF="list-cons.html#ANDDEFAULT"
367 >using an <I
368 CLASS="FIRSTTERM"
369 >and
370 list</I
371 > to supply a default command-line
372 argument</A
373 >.</P
374 ></DD
375 ><DT
376 ><TT
377 CLASS="USERINPUT"
378 ><B
379 >${parameter=default}</B
380 ></TT
381 >, <TT
382 CLASS="USERINPUT"
383 ><B
384 >${parameter:=default}</B
385 ></TT
386 ></DT
387 ><DD
388 ><P
389 ><A
390 NAME="DEFPARAM"
391 ></A
392 ></P
393 ><P
394 >If parameter not set, set it to
395 <I
396 CLASS="FIRSTTERM"
397 >default</I
398 >.</P
399 ><P
400 >Both forms nearly equivalent. The <SPAN
401 CLASS="TOKEN"
402 >:</SPAN
403 >
404 makes a difference only when <TT
405 CLASS="VARNAME"
406 >$parameter</TT
407 >
408 has been declared and is null,
409 <A
410 NAME="AEN6310"
411 HREF="#FTN.AEN6310"
412 >[1]</A
413 >
414 as above.
415 </P
416 ><P
417 ><TABLE
418 BORDER="0"
419 BGCOLOR="#E0E0E0"
420 WIDTH="90%"
421 ><TR
422 ><TD
423 ><PRE
424 CLASS="PROGRAMLISTING"
425 > 1&nbsp;echo ${var=abc} # abc
426 2&nbsp;echo ${var=xyz} # abc
427 3&nbsp;# $var had already been set to abc, so it did not change.</PRE
428 ></TD
429 ></TR
430 ></TABLE
431 ></P
432 ></DD
433 ><DT
434 ><A
435 NAME="PARAMALTV"
436 ></A
437 ><TT
438 CLASS="USERINPUT"
439 ><B
440 >${parameter+alt_value}</B
441 ></TT
442 >, <TT
443 CLASS="USERINPUT"
444 ><B
445 >${parameter:+alt_value}</B
446 ></TT
447 ></DT
448 ><DD
449 ><P
450 >If parameter set, use
451 <TT
452 CLASS="USERINPUT"
453 ><B
454 >alt_value</B
455 ></TT
456 >, else use null
457 string.</P
458 ><P
459 >Both forms nearly equivalent. The <SPAN
460 CLASS="TOKEN"
461 >:</SPAN
462 >
463 makes a difference only when
464 <TT
465 CLASS="PARAMETER"
466 ><I
467 >parameter</I
468 ></TT
469 >
470 has been declared and is null, see below.</P
471 ><P
472 ><TABLE
473 BORDER="0"
474 BGCOLOR="#E0E0E0"
475 WIDTH="90%"
476 ><TR
477 ><TD
478 ><PRE
479 CLASS="PROGRAMLISTING"
480 > 1&nbsp;echo "###### \${parameter+alt_value} ########"
481 2&nbsp;echo
482 3&nbsp;
483 4&nbsp;a=${param1+xyz}
484 5&nbsp;echo "a = $a" # a =
485 6&nbsp;
486 7&nbsp;param2=
487 8&nbsp;a=${param2+xyz}
488 9&nbsp;echo "a = $a" # a = xyz
489 10&nbsp;
490 11&nbsp;param3=123
491 12&nbsp;a=${param3+xyz}
492 13&nbsp;echo "a = $a" # a = xyz
493 14&nbsp;
494 15&nbsp;echo
495 16&nbsp;echo "###### \${parameter:+alt_value} ########"
496 17&nbsp;echo
497 18&nbsp;
498 19&nbsp;a=${param4:+xyz}
499 20&nbsp;echo "a = $a" # a =
500 21&nbsp;
501 22&nbsp;param5=
502 23&nbsp;a=${param5:+xyz}
503 24&nbsp;echo "a = $a" # a =
504 25&nbsp;# Different result from a=${param5+xyz}
505 26&nbsp;
506 27&nbsp;param6=123
507 28&nbsp;a=${param6:+xyz}
508 29&nbsp;echo "a = $a" # a = xyz</PRE
509 ></TD
510 ></TR
511 ></TABLE
512 ></P
513 ></DD
514 ><DT
515 ><A
516 NAME="QERRMSG"
517 ></A
518 ><TT
519 CLASS="USERINPUT"
520 ><B
521 >${parameter?err_msg}</B
522 ></TT
523 >, <TT
524 CLASS="USERINPUT"
525 ><B
526 >${parameter:?err_msg}</B
527 ></TT
528 ></DT
529 ><DD
530 ><P
531 >If parameter set, use it, else print
532 <I
533 CLASS="FIRSTTERM"
534 >err_msg</I
535 > and <SPAN
536 CLASS="emphasis"
537 ><I
538 CLASS="EMPHASIS"
539 >abort
540 the script</I
541 ></SPAN
542 > with an <A
543 HREF="exit-status.html#EXITSTATUSREF"
544 >exit status</A
545 > of
546 <SPAN
547 CLASS="ERRORCODE"
548 >1</SPAN
549 >.</P
550 ><P
551 >Both forms nearly equivalent. The <SPAN
552 CLASS="TOKEN"
553 >:</SPAN
554 >
555 makes a difference only when <TT
556 CLASS="PARAMETER"
557 ><I
558 >parameter</I
559 ></TT
560 >
561 has been declared and is null, as above.</P
562 ></DD
563 ></DL
564 ></DIV
565 ><DIV
566 CLASS="EXAMPLE"
567 ><HR><A
568 NAME="EX6"
569 ></A
570 ><P
571 ><B
572 >Example 10-7. Using parameter substitution and error messages</B
573 ></P
574 ><TABLE
575 BORDER="0"
576 BGCOLOR="#E0E0E0"
577 WIDTH="100%"
578 ><TR
579 ><TD
580 ><PRE
581 CLASS="PROGRAMLISTING"
582 > 1&nbsp;#!/bin/bash
583 2&nbsp;
584 3&nbsp;# Check some of the system's environmental variables.
585 4&nbsp;# This is good preventative maintenance.
586 5&nbsp;# If, for example, $USER, the name of the person at the console, is not set,
587 6&nbsp;#+ the machine will not recognize you.
588 7&nbsp;
589 8&nbsp;: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}
590 9&nbsp; echo
591 10&nbsp; echo "Name of the machine is $HOSTNAME."
592 11&nbsp; echo "You are $USER."
593 12&nbsp; echo "Your home directory is $HOME."
594 13&nbsp; echo "Your mail INBOX is located in $MAIL."
595 14&nbsp; echo
596 15&nbsp; echo "If you are reading this message,"
597 16&nbsp; echo "critical environmental variables have been set."
598 17&nbsp; echo
599 18&nbsp; echo
600 19&nbsp;
601 20&nbsp;# ------------------------------------------------------
602 21&nbsp;
603 22&nbsp;# The ${variablename?} construction can also check
604 23&nbsp;#+ for variables set within the script.
605 24&nbsp;
606 25&nbsp;ThisVariable=Value-of-ThisVariable
607 26&nbsp;# Note, by the way, that string variables may be set
608 27&nbsp;#+ to characters disallowed in their names.
609 28&nbsp;: ${ThisVariable?}
610 29&nbsp;echo "Value of ThisVariable is $ThisVariable".
611 30&nbsp;
612 31&nbsp;echo; echo
613 32&nbsp;
614 33&nbsp;
615 34&nbsp;: ${ZZXy23AB?"ZZXy23AB has not been set."}
616 35&nbsp;# Since ZZXy23AB has not been set,
617 36&nbsp;#+ then the script terminates with an error message.
618 37&nbsp;
619 38&nbsp;# You can specify the error message.
620 39&nbsp;# : ${variablename?"ERROR MESSAGE"}
621 40&nbsp;
622 41&nbsp;
623 42&nbsp;# Same result with: dummy_variable=${ZZXy23AB?}
624 43&nbsp;# dummy_variable=${ZZXy23AB?"ZXy23AB has not been set."}
625 44&nbsp;#
626 45&nbsp;# echo ${ZZXy23AB?} &#62;/dev/null
627 46&nbsp;
628 47&nbsp;# Compare these methods of checking whether a variable has been set
629 48&nbsp;#+ with "set -u" . . .
630 49&nbsp;
631 50&nbsp;
632 51&nbsp;
633 52&nbsp;echo "You will not see this message, because script already terminated."
634 53&nbsp;
635 54&nbsp;HERE=0
636 55&nbsp;exit $HERE # Will NOT exit here.
637 56&nbsp;
638 57&nbsp;# In fact, this script will return an exit status (echo $?) of 1.</PRE
639 ></TD
640 ></TR
641 ></TABLE
642 ><HR></DIV
643 ><DIV
644 CLASS="EXAMPLE"
645 ><HR><A
646 NAME="USAGEMESSAGE"
647 ></A
648 ><P
649 ><B
650 >Example 10-8. Parameter substitution and <SPAN
651 CLASS="QUOTE"
652 >"usage"</SPAN
653 > messages</B
654 ></P
655 ><TABLE
656 BORDER="0"
657 BGCOLOR="#E0E0E0"
658 WIDTH="100%"
659 ><TR
660 ><TD
661 ><PRE
662 CLASS="PROGRAMLISTING"
663 > 1&nbsp;#!/bin/bash
664 2&nbsp;# usage-message.sh
665 3&nbsp;
666 4&nbsp;: ${1?"Usage: $0 ARGUMENT"}
667 5&nbsp;# Script exits here if command-line parameter absent,
668 6&nbsp;#+ with following error message.
669 7&nbsp;# usage-message.sh: 1: Usage: usage-message.sh ARGUMENT
670 8&nbsp;
671 9&nbsp;echo "These two lines echo only if command-line parameter given."
672 10&nbsp;echo "command-line parameter = \"$1\""
673 11&nbsp;
674 12&nbsp;exit 0 # Will exit here only if command-line parameter present.
675 13&nbsp;
676 14&nbsp;# Check the exit status, both with and without command-line parameter.
677 15&nbsp;# If command-line parameter present, then "$?" is 0.
678 16&nbsp;# If not, then "$?" is 1.</PRE
679 ></TD
680 ></TR
681 ></TABLE
682 ><HR></DIV
683 ><DIV
684 CLASS="FORMALPARA"
685 ><P
686 ><B
687 >Parameter substitution and/or expansion. </B
688 ><A
689 NAME="PSUB2"
690 ></A
691 >The following expressions are
692 the complement to the <B
693 CLASS="COMMAND"
694 >match</B
695 >
696 <TT
697 CLASS="REPLACEABLE"
698 ><I
699 >in</I
700 ></TT
701 > <B
702 CLASS="COMMAND"
703 >expr</B
704 >
705 string operations (see <A
706 HREF="moreadv.html#EX45"
707 >Example 16-9</A
708 >).
709 These particular ones are used mostly in parsing file
710 path names.</P
711 ></DIV
712 ><DIV
713 CLASS="VARIABLELIST"
714 ><P
715 ><B
716 ><A
717 NAME="PSOREX1"
718 ></A
719 >Variable length / Substring removal</B
720 ></P
721 ><DL
722 ><DT
723 ><TT
724 CLASS="USERINPUT"
725 ><B
726 >${#var}</B
727 ></TT
728 ></DT
729 ><DD
730 ><P
731 ><TT
732 CLASS="USERINPUT"
733 ><B
734 >String length</B
735 ></TT
736 > (number
737 of characters in <TT
738 CLASS="VARNAME"
739 >$var</TT
740 >). For
741 an <A
742 HREF="arrays.html#ARRAYREF"
743 >array</A
744 >,
745 <B
746 CLASS="COMMAND"
747 >${#array}</B
748 > is the length of the
749 first element in the array.</P
750 ><DIV
751 CLASS="NOTE"
752 ><TABLE
753 CLASS="NOTE"
754 WIDTH="90%"
755 BORDER="0"
756 ><TR
757 ><TD
758 WIDTH="25"
759 ALIGN="CENTER"
760 VALIGN="TOP"
761 ><IMG
762 SRC="common/note.png"
763 HSPACE="5"
764 ALT="Note"></TD
765 ><TD
766 ALIGN="LEFT"
767 VALIGN="TOP"
768 ><P
769 > Exceptions:
770
771 <UL
772 ><LI
773 ><P
774 ><A
775 NAME="NUMPOSPARAM"
776 ></A
777 ></P
778 ><P
779 > <B
780 CLASS="COMMAND"
781 >${#*}</B
782 > and
783 <B
784 CLASS="COMMAND"
785 >${#@}</B
786 > give the <SPAN
787 CLASS="emphasis"
788 ><I
789 CLASS="EMPHASIS"
790 >number
791 of positional parameters</I
792 ></SPAN
793 >.
794 </P
795 ></LI
796 ><LI
797 ><P
798 > For an array, <B
799 CLASS="COMMAND"
800 >${#array[*]}</B
801 > and
802 <B
803 CLASS="COMMAND"
804 >${#array[@]}</B
805 > give the number
806 of elements in the array.
807 </P
808 ></LI
809 ></UL
810 >
811 </P
812 ></TD
813 ></TR
814 ></TABLE
815 ></DIV
816 ><DIV
817 CLASS="EXAMPLE"
818 ><HR><A
819 NAME="LENGTH"
820 ></A
821 ><P
822 ><B
823 >Example 10-9. Length of a variable</B
824 ></P
825 ><TABLE
826 BORDER="0"
827 BGCOLOR="#E0E0E0"
828 WIDTH="90%"
829 ><TR
830 ><TD
831 ><PRE
832 CLASS="PROGRAMLISTING"
833 > 1&nbsp;#!/bin/bash
834 2&nbsp;# length.sh
835 3&nbsp;
836 4&nbsp;E_NO_ARGS=65
837 5&nbsp;
838 6&nbsp;if [ $# -eq 0 ] # Must have command-line args to demo script.
839 7&nbsp;then
840 8&nbsp; echo "Please invoke this script with one or more command-line arguments."
841 9&nbsp; exit $E_NO_ARGS
842 10&nbsp;fi
843 11&nbsp;
844 12&nbsp;var01=abcdEFGH28ij
845 13&nbsp;echo "var01 = ${var01}"
846 14&nbsp;echo "Length of var01 = ${#var01}"
847 15&nbsp;# Now, let's try embedding a space.
848 16&nbsp;var02="abcd EFGH28ij"
849 17&nbsp;echo "var02 = ${var02}"
850 18&nbsp;echo "Length of var02 = ${#var02}"
851 19&nbsp;
852 20&nbsp;echo "Number of command-line arguments passed to script = ${#@}"
853 21&nbsp;echo "Number of command-line arguments passed to script = ${#*}"
854 22&nbsp;
855 23&nbsp;exit 0</PRE
856 ></TD
857 ></TR
858 ></TABLE
859 ><HR></DIV
860 ></DD
861 ><DT
862 ><A
863 NAME="PSOREX2"
864 ></A
865 ><TT
866 CLASS="USERINPUT"
867 ><B
868 >${var#Pattern}</B
869 ></TT
870 >, <TT
871 CLASS="USERINPUT"
872 ><B
873 >${var##Pattern}</B
874 ></TT
875 ></DT
876 ><DD
877 ><P
878 ><A
879 NAME="PSOREXSH"
880 ></A
881 ></P
882 ><P
883 ><B
884 CLASS="COMMAND"
885 >${var#Pattern} </B
886 >
887 Remove from <TT
888 CLASS="VARNAME"
889 >$var</TT
890 >
891 the <SPAN
892 CLASS="emphasis"
893 ><I
894 CLASS="EMPHASIS"
895 >shortest</I
896 ></SPAN
897 > part of
898 <TT
899 CLASS="VARNAME"
900 >$Pattern</TT
901 > that matches
902 the <TT
903 CLASS="REPLACEABLE"
904 ><I
905 >front end</I
906 ></TT
907 > of
908 <TT
909 CLASS="VARNAME"
910 >$var</TT
911 >.
912 </P
913 ><P
914 ><A
915 NAME="PSOREXLO"
916 ></A
917 ></P
918 ><P
919 ><B
920 CLASS="COMMAND"
921 >${var##Pattern} </B
922 >
923 Remove from <TT
924 CLASS="VARNAME"
925 >$var</TT
926 >
927 the <SPAN
928 CLASS="emphasis"
929 ><I
930 CLASS="EMPHASIS"
931 >longest</I
932 ></SPAN
933 > part of
934 <TT
935 CLASS="VARNAME"
936 >$Pattern</TT
937 > that matches
938 the <TT
939 CLASS="REPLACEABLE"
940 ><I
941 >front end</I
942 ></TT
943 > of
944 <TT
945 CLASS="VARNAME"
946 >$var</TT
947 >.
948 </P
949 ><P
950 >A usage illustration from <A
951 HREF="contributed-scripts.html#DAYSBETWEEN"
952 >Example A-7</A
953 >:
954 <TABLE
955 BORDER="0"
956 BGCOLOR="#E0E0E0"
957 WIDTH="90%"
958 ><TR
959 ><TD
960 ><PRE
961 CLASS="PROGRAMLISTING"
962 > 1&nbsp;# Function from "days-between.sh" example.
963 2&nbsp;# Strips leading zero(s) from argument passed.
964 3&nbsp;
965 4&nbsp;strip_leading_zero () # Strip possible leading zero(s)
966 5&nbsp;{ #+ from argument passed.
967 6&nbsp; return=${1#0} # The "1" refers to "$1" -- passed arg.
968 7&nbsp;} # The "0" is what to remove from "$1" -- strips zeros.</PRE
969 ></TD
970 ></TR
971 ></TABLE
972 >
973 </P
974 ><P
975 >Manfred Schwarb's more elaborate variation of the
976 above:</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;strip_leading_zero2 () # Strip possible leading zero(s), since otherwise
987 2&nbsp;{ # Bash will interpret such numbers as octal values.
988 3&nbsp; shopt -s extglob # Turn on extended globbing.
989 4&nbsp; local val=${1##+(0)} # Use local variable, longest matching series of 0's.
990 5&nbsp; shopt -u extglob # Turn off extended globbing.
991 6&nbsp; _strip_leading_zero2=${val:-0}
992 7&nbsp; # If input was 0, return 0 instead of "".
993 8&nbsp;}</PRE
994 ></TD
995 ></TR
996 ></TABLE
997 >
998 </P
999 ><P
1000 >Another usage illustration:</P
1001 ><P
1002 ><TABLE
1003 BORDER="0"
1004 BGCOLOR="#E0E0E0"
1005 WIDTH="90%"
1006 ><TR
1007 ><TD
1008 ><PRE
1009 CLASS="PROGRAMLISTING"
1010 > 1&nbsp;echo `basename $PWD` # Basename of current working directory.
1011 2&nbsp;echo "${PWD##*/}" # Basename of current working directory.
1012 3&nbsp;echo
1013 4&nbsp;echo `basename $0` # Name of script.
1014 5&nbsp;echo $0 # Name of script.
1015 6&nbsp;echo "${0##*/}" # Name of script.
1016 7&nbsp;echo
1017 8&nbsp;filename=test.data
1018 9&nbsp;echo "${filename##*.}" # data
1019 10&nbsp; # Extension of filename.</PRE
1020 ></TD
1021 ></TR
1022 ></TABLE
1023 >
1024 </P
1025 ></DD
1026 ><DT
1027 ><A
1028 NAME="PCTPATREF"
1029 ></A
1030 ><TT
1031 CLASS="USERINPUT"
1032 ><B
1033 >${var%Pattern}</B
1034 ></TT
1035 >, <TT
1036 CLASS="USERINPUT"
1037 ><B
1038 >${var%%Pattern}</B
1039 ></TT
1040 ></DT
1041 ><DD
1042 ><P
1043 ><A
1044 NAME="PCTREP1"
1045 ></A
1046 ></P
1047 ><P
1048 ><B
1049 CLASS="COMMAND"
1050 >${var%Pattern}</B
1051 >
1052 Remove from <TT
1053 CLASS="VARNAME"
1054 >$var</TT
1055 >
1056 the <SPAN
1057 CLASS="emphasis"
1058 ><I
1059 CLASS="EMPHASIS"
1060 >shortest</I
1061 ></SPAN
1062 > part of
1063 <TT
1064 CLASS="VARNAME"
1065 >$Pattern</TT
1066 > that matches
1067 the <TT
1068 CLASS="REPLACEABLE"
1069 ><I
1070 >back end</I
1071 ></TT
1072 > of
1073 <TT
1074 CLASS="VARNAME"
1075 >$var</TT
1076 >. </P
1077 ><P
1078 ><A
1079 NAME="PCTREP2"
1080 ></A
1081 ></P
1082 ><P
1083 ><B
1084 CLASS="COMMAND"
1085 >${var%%Pattern}</B
1086 >
1087 Remove from <TT
1088 CLASS="VARNAME"
1089 >$var</TT
1090 >
1091 the <SPAN
1092 CLASS="emphasis"
1093 ><I
1094 CLASS="EMPHASIS"
1095 >longest</I
1096 ></SPAN
1097 > part of
1098 <TT
1099 CLASS="VARNAME"
1100 >$Pattern</TT
1101 > that matches
1102 the <TT
1103 CLASS="REPLACEABLE"
1104 ><I
1105 >back end</I
1106 ></TT
1107 > of
1108 <TT
1109 CLASS="VARNAME"
1110 >$var</TT
1111 >. </P
1112 ></DD
1113 ></DL
1114 ></DIV
1115 ><P
1116 ><A
1117 HREF="bash2.html#BASH2REF"
1118 >Version 2</A
1119 > of Bash added
1120 additional options.</P
1121 ><DIV
1122 CLASS="EXAMPLE"
1123 ><HR><A
1124 NAME="PATTMATCHING"
1125 ></A
1126 ><P
1127 ><B
1128 >Example 10-10. Pattern matching in parameter substitution</B
1129 ></P
1130 ><TABLE
1131 BORDER="0"
1132 BGCOLOR="#E0E0E0"
1133 WIDTH="100%"
1134 ><TR
1135 ><TD
1136 ><PRE
1137 CLASS="PROGRAMLISTING"
1138 > 1&nbsp;#!/bin/bash
1139 2&nbsp;# patt-matching.sh
1140 3&nbsp;
1141 4&nbsp;# Pattern matching using the # ## % %% parameter substitution operators.
1142 5&nbsp;
1143 6&nbsp;var1=abcd12345abc6789
1144 7&nbsp;pattern1=a*c # * (wild card) matches everything between a - c.
1145 8&nbsp;
1146 9&nbsp;echo
1147 10&nbsp;echo "var1 = $var1" # abcd12345abc6789
1148 11&nbsp;echo "var1 = ${var1}" # abcd12345abc6789
1149 12&nbsp; # (alternate form)
1150 13&nbsp;echo "Number of characters in ${var1} = ${#var1}"
1151 14&nbsp;echo
1152 15&nbsp;
1153 16&nbsp;echo "pattern1 = $pattern1" # a*c (everything between 'a' and 'c')
1154 17&nbsp;echo "--------------"
1155 18&nbsp;echo '${var1#$pattern1} =' "${var1#$pattern1}" # d12345abc6789
1156 19&nbsp;# Shortest possible match, strips out first 3 characters abcd12345abc6789
1157 20&nbsp;# ^^^^^ |-|
1158 21&nbsp;echo '${var1##$pattern1} =' "${var1##$pattern1}" # 6789
1159 22&nbsp;# Longest possible match, strips out first 12 characters abcd12345abc6789
1160 23&nbsp;# ^^^^^ |----------|
1161 24&nbsp;
1162 25&nbsp;echo; echo; echo
1163 26&nbsp;
1164 27&nbsp;pattern2=b*9 # everything between 'b' and '9'
1165 28&nbsp;echo "var1 = $var1" # Still abcd12345abc6789
1166 29&nbsp;echo
1167 30&nbsp;echo "pattern2 = $pattern2"
1168 31&nbsp;echo "--------------"
1169 32&nbsp;echo '${var1%pattern2} =' "${var1%$pattern2}" # abcd12345a
1170 33&nbsp;# Shortest possible match, strips out last 6 characters abcd12345abc6789
1171 34&nbsp;# ^^^^ |----|
1172 35&nbsp;echo '${var1%%pattern2} =' "${var1%%$pattern2}" # a
1173 36&nbsp;# Longest possible match, strips out last 12 characters abcd12345abc6789
1174 37&nbsp;# ^^^^ |-------------|
1175 38&nbsp;
1176 39&nbsp;# Remember, # and ## work from the left end (beginning) of string,
1177 40&nbsp;# % and %% work from the right end.
1178 41&nbsp;
1179 42&nbsp;echo
1180 43&nbsp;
1181 44&nbsp;exit 0</PRE
1182 ></TD
1183 ></TR
1184 ></TABLE
1185 ><HR></DIV
1186 ><DIV
1187 CLASS="EXAMPLE"
1188 ><HR><A
1189 NAME="RFE"
1190 ></A
1191 ><P
1192 ><B
1193 >Example 10-11. Renaming file extensions<SPAN
1194 CLASS="TOKEN"
1195 >:</SPAN
1196 ></B
1197 ></P
1198 ><TABLE
1199 BORDER="0"
1200 BGCOLOR="#E0E0E0"
1201 WIDTH="100%"
1202 ><TR
1203 ><TD
1204 ><PRE
1205 CLASS="PROGRAMLISTING"
1206 > 1&nbsp;#!/bin/bash
1207 2&nbsp;# rfe.sh: Renaming file extensions.
1208 3&nbsp;#
1209 4&nbsp;# rfe old_extension new_extension
1210 5&nbsp;#
1211 6&nbsp;# Example:
1212 7&nbsp;# To rename all *.gif files in working directory to *.jpg,
1213 8&nbsp;# rfe gif jpg
1214 9&nbsp;
1215 10&nbsp;
1216 11&nbsp;E_BADARGS=65
1217 12&nbsp;
1218 13&nbsp;case $# in
1219 14&nbsp; 0|1) # The vertical bar means "or" in this context.
1220 15&nbsp; echo "Usage: `basename $0` old_file_suffix new_file_suffix"
1221 16&nbsp; exit $E_BADARGS # If 0 or 1 arg, then bail out.
1222 17&nbsp; ;;
1223 18&nbsp;esac
1224 19&nbsp;
1225 20&nbsp;
1226 21&nbsp;for filename in *.$1
1227 22&nbsp;# Traverse list of files ending with 1st argument.
1228 23&nbsp;do
1229 24&nbsp; mv $filename ${filename%$1}$2
1230 25&nbsp; # Strip off part of filename matching 1st argument,
1231 26&nbsp; #+ then append 2nd argument.
1232 27&nbsp;done
1233 28&nbsp;
1234 29&nbsp;exit 0</PRE
1235 ></TD
1236 ></TR
1237 ></TABLE
1238 ><HR></DIV
1239 ><DIV
1240 CLASS="VARIABLELIST"
1241 ><P
1242 ><B
1243 ><A
1244 NAME="EXPREPL1"
1245 ></A
1246 >Variable expansion / Substring
1247 replacement</B
1248 ></P
1249 ><DL
1250 ><DT
1251 ></DT
1252 ><DD
1253 ><P
1254 >These constructs have been adopted from
1255 <I
1256 CLASS="FIRSTTERM"
1257 >ksh</I
1258 >.</P
1259 ></DD
1260 ><DT
1261 ><TT
1262 CLASS="USERINPUT"
1263 ><B
1264 >${var:pos}</B
1265 ></TT
1266 ></DT
1267 ><DD
1268 ><P
1269 >Variable <TT
1270 CLASS="REPLACEABLE"
1271 ><I
1272 >var</I
1273 ></TT
1274 > expanded,
1275 starting from offset <TT
1276 CLASS="REPLACEABLE"
1277 ><I
1278 >pos</I
1279 ></TT
1280 >.
1281 </P
1282 ></DD
1283 ><DT
1284 ><TT
1285 CLASS="USERINPUT"
1286 ><B
1287 >${var:pos:len}</B
1288 ></TT
1289 ></DT
1290 ><DD
1291 ><P
1292 >Expansion to a max of <TT
1293 CLASS="REPLACEABLE"
1294 ><I
1295 >len</I
1296 ></TT
1297 >
1298 characters of variable <TT
1299 CLASS="REPLACEABLE"
1300 ><I
1301 >var</I
1302 ></TT
1303 >, from offset
1304 <TT
1305 CLASS="REPLACEABLE"
1306 ><I
1307 >pos</I
1308 ></TT
1309 >. See <A
1310 HREF="contributed-scripts.html#PW"
1311 >Example A-13</A
1312 >
1313 for an example of the creative use of this operator.
1314 </P
1315 ></DD
1316 ><DT
1317 ><TT
1318 CLASS="USERINPUT"
1319 ><B
1320 >${var/Pattern/Replacement}</B
1321 ></TT
1322 ></DT
1323 ><DD
1324 ><P
1325 >First match of <TT
1326 CLASS="REPLACEABLE"
1327 ><I
1328 >Pattern</I
1329 ></TT
1330 >,
1331 within <TT
1332 CLASS="REPLACEABLE"
1333 ><I
1334 >var</I
1335 ></TT
1336 > replaced with
1337 <TT
1338 CLASS="REPLACEABLE"
1339 ><I
1340 >Replacement</I
1341 ></TT
1342 >.</P
1343 ><P
1344 >If <TT
1345 CLASS="REPLACEABLE"
1346 ><I
1347 >Replacement</I
1348 ></TT
1349 > is
1350 omitted, then the first match of
1351 <TT
1352 CLASS="REPLACEABLE"
1353 ><I
1354 >Pattern</I
1355 ></TT
1356 > is replaced by
1357 <SPAN
1358 CLASS="emphasis"
1359 ><I
1360 CLASS="EMPHASIS"
1361 >nothing</I
1362 ></SPAN
1363 >, that is, deleted.</P
1364 ></DD
1365 ><DT
1366 ><TT
1367 CLASS="USERINPUT"
1368 ><B
1369 >${var//Pattern/Replacement}</B
1370 ></TT
1371 ></DT
1372 ><DD
1373 ><DIV
1374 CLASS="FORMALPARA"
1375 ><P
1376 ><B
1377 >Global replacement. </B
1378 ><A
1379 NAME="PSGLOB"
1380 ></A
1381 >
1382 All matches of <TT
1383 CLASS="REPLACEABLE"
1384 ><I
1385 >Pattern</I
1386 ></TT
1387 >,
1388 within <TT
1389 CLASS="REPLACEABLE"
1390 ><I
1391 >var</I
1392 ></TT
1393 > replaced with
1394 <TT
1395 CLASS="REPLACEABLE"
1396 ><I
1397 >Replacement</I
1398 ></TT
1399 >.</P
1400 ></DIV
1401 ><P
1402 >As above, if <TT
1403 CLASS="REPLACEABLE"
1404 ><I
1405 >Replacement</I
1406 ></TT
1407 >
1408 is omitted, then all occurrences of
1409 <TT
1410 CLASS="REPLACEABLE"
1411 ><I
1412 >Pattern</I
1413 ></TT
1414 > are replaced by
1415 <SPAN
1416 CLASS="emphasis"
1417 ><I
1418 CLASS="EMPHASIS"
1419 >nothing</I
1420 ></SPAN
1421 >, that is, deleted.</P
1422 ><DIV
1423 CLASS="EXAMPLE"
1424 ><HR><A
1425 NAME="EX7"
1426 ></A
1427 ><P
1428 ><B
1429 >Example 10-12. Using pattern matching to parse arbitrary strings</B
1430 ></P
1431 ><TABLE
1432 BORDER="0"
1433 BGCOLOR="#E0E0E0"
1434 WIDTH="90%"
1435 ><TR
1436 ><TD
1437 ><PRE
1438 CLASS="PROGRAMLISTING"
1439 > 1&nbsp;#!/bin/bash
1440 2&nbsp;
1441 3&nbsp;var1=abcd-1234-defg
1442 4&nbsp;echo "var1 = $var1"
1443 5&nbsp;
1444 6&nbsp;t=${var1#*-*}
1445 7&nbsp;echo "var1 (with everything, up to and including first - stripped out) = $t"
1446 8&nbsp;# t=${var1#*-} works just the same,
1447 9&nbsp;#+ since # matches the shortest string,
1448 10&nbsp;#+ and * matches everything preceding, including an empty string.
1449 11&nbsp;# (Thanks, Stephane Chazelas, for pointing this out.)
1450 12&nbsp;
1451 13&nbsp;t=${var1##*-*}
1452 14&nbsp;echo "If var1 contains a \"-\", returns empty string... var1 = $t"
1453 15&nbsp;
1454 16&nbsp;
1455 17&nbsp;t=${var1%*-*}
1456 18&nbsp;echo "var1 (with everything from the last - on stripped out) = $t"
1457 19&nbsp;
1458 20&nbsp;echo
1459 21&nbsp;
1460 22&nbsp;# -------------------------------------------
1461 23&nbsp;path_name=/home/bozo/ideas/thoughts.for.today
1462 24&nbsp;# -------------------------------------------
1463 25&nbsp;echo "path_name = $path_name"
1464 26&nbsp;t=${path_name##/*/}
1465 27&nbsp;echo "path_name, stripped of prefixes = $t"
1466 28&nbsp;# Same effect as t=`basename $path_name` in this particular case.
1467 29&nbsp;# t=${path_name%/}; t=${t##*/} is a more general solution,
1468 30&nbsp;#+ but still fails sometimes.
1469 31&nbsp;# If $path_name ends with a newline, then `basename $path_name` will not work,
1470 32&nbsp;#+ but the above expression will.
1471 33&nbsp;# (Thanks, S.C.)
1472 34&nbsp;
1473 35&nbsp;t=${path_name%/*.*}
1474 36&nbsp;# Same effect as t=`dirname $path_name`
1475 37&nbsp;echo "path_name, stripped of suffixes = $t"
1476 38&nbsp;# These will fail in some cases, such as "../", "/foo////", # "foo/", "/".
1477 39&nbsp;# Removing suffixes, especially when the basename has no suffix,
1478 40&nbsp;#+ but the dirname does, also complicates matters.
1479 41&nbsp;# (Thanks, S.C.)
1480 42&nbsp;
1481 43&nbsp;echo
1482 44&nbsp;
1483 45&nbsp;t=${path_name:11}
1484 46&nbsp;echo "$path_name, with first 11 chars stripped off = $t"
1485 47&nbsp;t=${path_name:11:5}
1486 48&nbsp;echo "$path_name, with first 11 chars stripped off, length 5 = $t"
1487 49&nbsp;
1488 50&nbsp;echo
1489 51&nbsp;
1490 52&nbsp;t=${path_name/bozo/clown}
1491 53&nbsp;echo "$path_name with \"bozo\" replaced by \"clown\" = $t"
1492 54&nbsp;t=${path_name/today/}
1493 55&nbsp;echo "$path_name with \"today\" deleted = $t"
1494 56&nbsp;t=${path_name//o/O}
1495 57&nbsp;echo "$path_name with all o's capitalized = $t"
1496 58&nbsp;t=${path_name//o/}
1497 59&nbsp;echo "$path_name with all o's deleted = $t"
1498 60&nbsp;
1499 61&nbsp;exit 0</PRE
1500 ></TD
1501 ></TR
1502 ></TABLE
1503 ><HR></DIV
1504 ></DD
1505 ><DT
1506 ><TT
1507 CLASS="USERINPUT"
1508 ><B
1509 >${var/#Pattern/Replacement}</B
1510 ></TT
1511 ></DT
1512 ><DD
1513 ><P
1514 >If <I
1515 CLASS="FIRSTTERM"
1516 >prefix</I
1517 > of
1518 <TT
1519 CLASS="REPLACEABLE"
1520 ><I
1521 >var</I
1522 ></TT
1523 > matches
1524 <TT
1525 CLASS="REPLACEABLE"
1526 ><I
1527 >Pattern</I
1528 ></TT
1529 >, then substitute
1530 <TT
1531 CLASS="REPLACEABLE"
1532 ><I
1533 >Replacement</I
1534 ></TT
1535 > for
1536 <TT
1537 CLASS="REPLACEABLE"
1538 ><I
1539 >Pattern</I
1540 ></TT
1541 >.</P
1542 ></DD
1543 ><DT
1544 ><TT
1545 CLASS="USERINPUT"
1546 ><B
1547 >${var/%Pattern/Replacement}</B
1548 ></TT
1549 ></DT
1550 ><DD
1551 ><P
1552 >If <I
1553 CLASS="FIRSTTERM"
1554 >suffix</I
1555 > of
1556 <TT
1557 CLASS="REPLACEABLE"
1558 ><I
1559 >var</I
1560 ></TT
1561 > matches
1562 <TT
1563 CLASS="REPLACEABLE"
1564 ><I
1565 >Pattern</I
1566 ></TT
1567 >, then substitute
1568 <TT
1569 CLASS="REPLACEABLE"
1570 ><I
1571 >Replacement</I
1572 ></TT
1573 > for
1574 <TT
1575 CLASS="REPLACEABLE"
1576 ><I
1577 >Pattern</I
1578 ></TT
1579 >.</P
1580 ><DIV
1581 CLASS="EXAMPLE"
1582 ><HR><A
1583 NAME="VARMATCH"
1584 ></A
1585 ><P
1586 ><B
1587 >Example 10-13. Matching patterns at prefix or suffix of string</B
1588 ></P
1589 ><TABLE
1590 BORDER="0"
1591 BGCOLOR="#E0E0E0"
1592 WIDTH="90%"
1593 ><TR
1594 ><TD
1595 ><PRE
1596 CLASS="PROGRAMLISTING"
1597 > 1&nbsp;#!/bin/bash
1598 2&nbsp;# var-match.sh:
1599 3&nbsp;# Demo of pattern replacement at prefix / suffix of string.
1600 4&nbsp;
1601 5&nbsp;v0=abc1234zip1234abc # Original variable.
1602 6&nbsp;echo "v0 = $v0" # abc1234zip1234abc
1603 7&nbsp;echo
1604 8&nbsp;
1605 9&nbsp;# Match at prefix (beginning) of string.
1606 10&nbsp;v1=${v0/#abc/ABCDEF} # abc1234zip1234abc
1607 11&nbsp; # |-|
1608 12&nbsp;echo "v1 = $v1" # ABCDEF1234zip1234abc
1609 13&nbsp; # |----|
1610 14&nbsp;
1611 15&nbsp;# Match at suffix (end) of string.
1612 16&nbsp;v2=${v0/%abc/ABCDEF} # abc1234zip123abc
1613 17&nbsp; # |-|
1614 18&nbsp;echo "v2 = $v2" # abc1234zip1234ABCDEF
1615 19&nbsp; # |----|
1616 20&nbsp;
1617 21&nbsp;echo
1618 22&nbsp;
1619 23&nbsp;# ----------------------------------------------------
1620 24&nbsp;# Must match at beginning / end of string,
1621 25&nbsp;#+ otherwise no replacement results.
1622 26&nbsp;# ----------------------------------------------------
1623 27&nbsp;v3=${v0/#123/000} # Matches, but not at beginning.
1624 28&nbsp;echo "v3 = $v3" # abc1234zip1234abc
1625 29&nbsp; # NO REPLACEMENT.
1626 30&nbsp;v4=${v0/%123/000} # Matches, but not at end.
1627 31&nbsp;echo "v4 = $v4" # abc1234zip1234abc
1628 32&nbsp; # NO REPLACEMENT.
1629 33&nbsp;
1630 34&nbsp;exit 0 </PRE
1631 ></TD
1632 ></TR
1633 ></TABLE
1634 ><HR></DIV
1635 ></DD
1636 ><DT
1637 ><A
1638 NAME="VARPREFIXM"
1639 ></A
1640 ><TT
1641 CLASS="USERINPUT"
1642 ><B
1643 >${!varprefix*}</B
1644 ></TT
1645 >, <TT
1646 CLASS="USERINPUT"
1647 ><B
1648 >${!varprefix@}</B
1649 ></TT
1650 ></DT
1651 ><DD
1652 ><P
1653 >Matches <SPAN
1654 CLASS="emphasis"
1655 ><I
1656 CLASS="EMPHASIS"
1657 >names</I
1658 ></SPAN
1659 > of all
1660 previously declared variables beginning
1661 with <TT
1662 CLASS="PARAMETER"
1663 ><I
1664 >varprefix</I
1665 ></TT
1666 >.
1667 <TABLE
1668 BORDER="0"
1669 BGCOLOR="#E0E0E0"
1670 WIDTH="90%"
1671 ><TR
1672 ><TD
1673 ><PRE
1674 CLASS="PROGRAMLISTING"
1675 > 1&nbsp;# This is a variation on indirect reference, but with a * or @.
1676 2&nbsp;# Bash, version 2.04, adds this feature.
1677 3&nbsp;
1678 4&nbsp;xyz23=whatever
1679 5&nbsp;xyz24=
1680 6&nbsp;
1681 7&nbsp;a=${!xyz*} # Expands to *names* of declared variables
1682 8&nbsp;# ^ ^ ^ + beginning with "xyz".
1683 9&nbsp;echo "a = $a" # a = xyz23 xyz24
1684 10&nbsp;a=${!xyz@} # Same as above.
1685 11&nbsp;echo "a = $a" # a = xyz23 xyz24
1686 12&nbsp;
1687 13&nbsp;echo "---"
1688 14&nbsp;
1689 15&nbsp;abc23=something_else
1690 16&nbsp;b=${!abc*}
1691 17&nbsp;echo "b = $b" # b = abc23
1692 18&nbsp;c=${!b} # Now, the more familiar type of indirect reference.
1693 19&nbsp;echo $c # something_else</PRE
1694 ></TD
1695 ></TR
1696 ></TABLE
1697 >
1698 </P
1699 ></DD
1700 ></DL
1701 ></DIV
1702 ></DIV
1703 ><H3
1704 CLASS="FOOTNOTES"
1705 >Notes</H3
1706 ><TABLE
1707 BORDER="0"
1708 CLASS="FOOTNOTES"
1709 WIDTH="100%"
1710 ><TR
1711 ><TD
1712 ALIGN="LEFT"
1713 VALIGN="TOP"
1714 WIDTH="5%"
1715 ><A
1716 NAME="FTN.AEN6310"
1717 HREF="parameter-substitution.html#AEN6310"
1718 >[1]</A
1719 ></TD
1720 ><TD
1721 ALIGN="LEFT"
1722 VALIGN="TOP"
1723 WIDTH="95%"
1724 ><P
1725 >If $parameter is null in a
1726 non-interactive script, it will terminate with a <A
1727 HREF="exitcodes.html#EXITCODESREF"
1728 ><SPAN
1729 CLASS="RETURNVALUE"
1730 >127</SPAN
1731 >
1732 exit status</A
1733 > (the Bash error code for
1734 <SPAN
1735 CLASS="QUOTE"
1736 >"command not found"</SPAN
1737 >).</P
1738 ></TD
1739 ></TR
1740 ></TABLE
1741 ><DIV
1742 CLASS="NAVFOOTER"
1743 ><HR
1744 ALIGN="LEFT"
1745 WIDTH="100%"><TABLE
1746 SUMMARY="Footer navigation table"
1747 WIDTH="100%"
1748 BORDER="0"
1749 CELLPADDING="0"
1750 CELLSPACING="0"
1751 ><TR
1752 ><TD
1753 WIDTH="33%"
1754 ALIGN="left"
1755 VALIGN="top"
1756 ><A
1757 HREF="manipulatingvars.html"
1758 ACCESSKEY="P"
1759 >Prev</A
1760 ></TD
1761 ><TD
1762 WIDTH="34%"
1763 ALIGN="center"
1764 VALIGN="top"
1765 ><A
1766 HREF="index.html"
1767 ACCESSKEY="H"
1768 >Home</A
1769 ></TD
1770 ><TD
1771 WIDTH="33%"
1772 ALIGN="right"
1773 VALIGN="top"
1774 ><A
1775 HREF="loops.html"
1776 ACCESSKEY="N"
1777 >Next</A
1778 ></TD
1779 ></TR
1780 ><TR
1781 ><TD
1782 WIDTH="33%"
1783 ALIGN="left"
1784 VALIGN="top"
1785 >Manipulating Variables</TD
1786 ><TD
1787 WIDTH="34%"
1788 ALIGN="center"
1789 VALIGN="top"
1790 ><A
1791 HREF="manipulatingvars.html"
1792 ACCESSKEY="U"
1793 >Up</A
1794 ></TD
1795 ><TD
1796 WIDTH="33%"
1797 ALIGN="right"
1798 VALIGN="top"
1799 >Loops and Branches</TD
1800 ></TR
1801 ></TABLE
1802 ></DIV
1803 ></BODY
1804 ></HTML
1805 >