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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Command Substitution</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="Beyond the Basics"
HREF="part3.html"><LINK
REL="PREVIOUS"
TITLE="Testing and Branching"
HREF="testbranch.html"><LINK
REL="NEXT"
TITLE="Arithmetic Expansion"
HREF="arithexp.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="CHAPTER"
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="testbranch.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="arithexp.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="COMMANDSUB"
></A
>Chapter 12. Command Substitution</H1
><P
> <A
NAME="COMMANDSUBREF"
></A
><B
CLASS="COMMAND"
>Command
substitution</B
> reassigns the output of a command
<A
NAME="AEN7205"
HREF="#FTN.AEN7205"
>[1]</A
>
or even multiple commands; it literally plugs the command
output into another context.
<A
NAME="AEN7211"
HREF="#FTN.AEN7211"
>[2]</A
>
</P
><P
><A
NAME="BACKQUOTESREF"
></A
>The classic form of command
substitution uses <I
CLASS="FIRSTTERM"
>backquotes</I
>
(`...`). Commands within backquotes (backticks) generate
command-line text.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 script_name=`basename $0`
2 echo "The name of this script is $script_name."</PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="FORMALPARA"
><P
><B
>The output of commands can be used as arguments to
another command, to set a variable, and even for generating
the argument list in a <A
HREF="loops.html#FORLOOPREF1"
>for</A
>
loop. </B
></P
></DIV
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 rm `cat filename` # <SPAN
CLASS="QUOTE"
>"filename"</SPAN
> contains a list of files to delete.
2 #
3 # S. C. points out that "arg list too long" error might result.
4 # Better is xargs rm -- < filename
5 # ( -- covers those cases where <SPAN
CLASS="QUOTE"
>"filename"</SPAN
> begins with a <SPAN
CLASS="QUOTE"
>"-"</SPAN
> )
6
7 textfile_listing=`ls *.txt`
8 # Variable contains names of all *.txt files in current working directory.
9 echo $textfile_listing
10
11 textfile_listing2=$(ls *.txt) # The alternative form of command substitution.
12 echo $textfile_listing2
13 # Same result.
14
15 # A possible problem with putting a list of files into a single string
16 # is that a newline may creep in.
17 #
18 # A safer way to assign a list of files to a parameter is with an array.
19 # shopt -s nullglob # If no match, filename expands to nothing.
20 # textfile_listing=( *.txt )
21 #
22 # Thanks, S.C.</PRE
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="CSSUBSH"
></A
>Command substitution
invokes a <A
HREF="subshells.html#SUBSHELLSREF"
>subshell</A
>.</P
></TD
></TR
></TABLE
></DIV
><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
><A
NAME="CSWS"
></A
>Command substitution may
result in <A
HREF="quoting.html#WSPLITREF"
>word splitting</A
>.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 COMMAND `echo a b` # 2 args: a and b
2
3 COMMAND "`echo a b`" # 1 arg: "a b"
4
5 COMMAND `echo` # no arg
6
7 COMMAND "`echo`" # one empty arg
8
9
10 # Thanks, S.C.</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="CSTRNL"
></A
></P
><P
>Even when there is no word splitting, command
substitution can remove trailing newlines.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # cd "`pwd`" # This should always work.
2 # However...
3
4 mkdir 'dir with trailing newline
5 '
6
7 cd 'dir with trailing newline
8 '
9
10 cd "`pwd`" # Error message:
11 # bash: cd: /tmp/file with trailing newline: No such file or directory
12
13 cd "$PWD" # Works fine.
14
15
16
17
18
19 old_tty_setting=$(stty -g) # Save old terminal setting.
20 echo "Hit a key "
21 stty -icanon -echo # Disable "canonical" mode for terminal.
22 # Also, disable *local* echo.
23 key=$(dd bs=1 count=1 2> /dev/null) # Using 'dd' to get a keypress.
24 stty "$old_tty_setting" # Restore old setting.
25 echo "You hit ${#key} key." # ${#variable} = number of characters in $variable
26 #
27 # Hit any key except RETURN, and the output is "You hit 1 key."
28 # Hit RETURN, and it's "You hit 0 key."
29 # The newline gets eaten in the command substitution.
30
31 #Code snippet by Stphane Chazelas.</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
><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
>Using <B
CLASS="COMMAND"
>echo</B
> to output an
<I
CLASS="FIRSTTERM"
>unquoted</I
> variable set with command
substitution removes trailing newlines characters from
the output of the reassigned command(s). This can cause
unpleasant surprises.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 dir_listing=`ls -l`
2 echo $dir_listing # unquoted
3
4 # Expecting a nicely ordered directory listing.
5
6 # However, what you get is:
7 # total 3 -rw-rw-r-- 1 bozo bozo 30 May 13 17:15 1.txt -rw-rw-r-- 1 bozo
8 # bozo 51 May 15 20:57 t2.sh -rwxr-xr-x 1 bozo bozo 217 Mar 5 21:13 wi.sh
9
10 # The newlines disappeared.
11
12
13 echo "$dir_listing" # quoted
14 # -rw-rw-r-- 1 bozo 30 May 13 17:15 1.txt
15 # -rw-rw-r-- 1 bozo 51 May 15 20:57 t2.sh
16 # -rwxr-xr-x 1 bozo 217 Mar 5 21:13 wi.sh</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
><P
>Command substitution even permits setting a variable to the
contents of a file, using either <A
HREF="io-redirection.html#IOREDIRREF"
>redirection</A
> or the <A
HREF="external.html#CATREF"
>cat</A
> command.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 variable1=`<file1` # Set "variable1" to contents of "file1".
2 variable2=`cat file2` # Set "variable2" to contents of "file2".
3 # This, however, forks a new process,
4 #+ so the line of code executes slower than the above version.
5
6 # Note that the variables may contain embedded whitespace,
7 #+ or even (horrors), control characters.
8
9 # It is not necessary to explicitly assign a variable.
10 echo "` <$0`" # Echoes the script itself to stdout.</PRE
></TD
></TR
></TABLE
>
</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Excerpts from system file, /etc/rc.d/rc.sysinit
2 #+ (on a Red Hat Linux installation)
3
4
5 if [ -f /fsckoptions ]; then
6 fsckoptions=`cat /fsckoptions`
7 ...
8 fi
9 #
10 #
11 if [ -e "/proc/ide/${disk[$device]}/media" ] ; then
12 hdmedia=`cat /proc/ide/${disk[$device]}/media`
13 ...
14 fi
15 #
16 #
17 if [ ! -n "`uname -r | grep -- "-"`" ]; then
18 ktag="`cat /proc/version`"
19 ...
20 fi
21 #
22 #
23 if [ $usb = "1" ]; then
24 sleep 5
25 mouseoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep -E "^I.*Cls=03.*Prot=02"`
26 kbdoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep -E "^I.*Cls=03.*Prot=01"`
27 ...
28 fi</PRE
></TD
></TR
></TABLE
>
</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
>Do not set a variable to the contents of a
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>long</I
></SPAN
> text file unless you have a very good
reason for doing so. Do not set a variable to the contents of a
<I
CLASS="FIRSTTERM"
>binary</I
> file, even as a joke.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="STUPSCR"
></A
><P
><B
>Example 12-1. Stupid script tricks</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # stupid-script-tricks.sh: Don't try this at home, folks.
3 # From "Stupid Script Tricks," Volume I.
4
5 exit 99 ### Comment out this line if you dare.
6
7 dangerous_variable=`cat /boot/vmlinuz` # The compressed Linux kernel itself.
8
9 echo "string-length of \$dangerous_variable = ${#dangerous_variable}"
10 # string-length of $dangerous_variable = 794151
11 # (Newer kernels are bigger.)
12 # Does not give same count as 'wc -c /boot/vmlinuz'.
13
14 # echo "$dangerous_variable"
15 # Don't try this! It would hang the script.
16
17
18 # The document author is aware of no useful applications for
19 #+ setting a variable to the contents of a binary file.
20
21 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>Notice that a <I
CLASS="FIRSTTERM"
>buffer overrun</I
>
does not occur. This is one instance where an interpreted
language, such as Bash, provides more protection from
programmer mistakes than a compiled language.</P
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="CSVL"
></A
></P
><P
>Command substitution permits setting a variable to the
output of a <A
HREF="loops.html#FORLOOPREF1"
>loop</A
>. The
key to this is grabbing the output of an <A
HREF="internal.html#ECHOREF"
>echo</A
> command within the
loop.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="CSUBLOOP"
></A
><P
><B
>Example 12-2. Generating a variable from a loop</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # csubloop.sh: Setting a variable to the output of a loop.
3
4 variable1=`for i in 1 2 3 4 5
5 do
6 echo -n "$i" # The 'echo' command is critical
7 done` #+ to command substitution here.
8
9 echo "variable1 = $variable1" # variable1 = 12345
10
11
12 i=0
13 variable2=`while [ "$i" -lt 10 ]
14 do
15 echo -n "$i" # Again, the necessary 'echo'.
16 let "i += 1" # Increment.
17 done`
18
19 echo "variable2 = $variable2" # variable2 = 0123456789
20
21 # Demonstrates that it's possible to embed a loop
22 #+ inside a variable declaration.
23
24 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="CSTOOLSET"
></A
></P
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN7273"
></A
><P
>Command substitution makes it possible to extend the
toolset available to Bash. It is simply a matter
of writing a program or script that outputs to
<TT
CLASS="FILENAME"
>stdout</TT
> (like a well-behaved UNIX
tool should) and assigning that output to a variable.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #include <stdio.h>
2
3 /* "Hello, world." C program */
4
5 int main()
6 {
7 printf( "Hello, world.\n" );
8 return (0);
9 }</PRE
></TD
></TR
></TABLE
>
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>gcc -o hello hello.c</B
></TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # hello.sh
3
4 greeting=`./hello`
5 echo $greeting</PRE
></TD
></TR
></TABLE
>
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>sh hello.sh</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>Hello, world.</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
></DIV
></TD
></TR
></TABLE
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="CSPARENS"
></A
>The <B
CLASS="COMMAND"
>$(...)</B
>
form has superseded backticks for command
substitution.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 output=$(sed -n /"$1"/p $file) # From "grp.sh" example.
2
3 # Setting a variable to the contents of a text file.
4 File_contents1=$(cat $file1)
5 File_contents2=$(<$file2) # Bash permits this also.</PRE
></TD
></TR
></TABLE
></P
><P
>The <B
CLASS="COMMAND"
>$(...)</B
> form of command substitution
treats a double backslash in a different way than
<B
CLASS="COMMAND"
>`...`</B
>.</P
><P
>
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo `echo \\`</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
></TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo $(echo \\)</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>\</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="CSNEST"
></A
></P
><P
>The <B
CLASS="COMMAND"
>$(...)</B
> form of command
substitution permits nesting.
<A
NAME="AEN7308"
HREF="#FTN.AEN7308"
>[3]</A
>
</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 word_count=$( wc -w $(echo * | awk '{print $8}') )</PRE
></TD
></TR
></TABLE
>
</P
><P
>Or, for something a bit more elaborate . . .</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="AGRAM2"
></A
><P
><B
>Example 12-3. Finding anagrams</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # agram2.sh
3 # Example of nested command substitution.
4
5 # Uses "anagram" utility
6 #+ that is part of the author's "yawl" word list package.
7 # http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz
8 # http://bash.deta.in/yawl-0.3.2.tar.gz
9
10 E_NOARGS=86
11 E_BADARG=87
12 MINLEN=7
13
14 if [ -z "$1" ]
15 then
16 echo "Usage $0 LETTERSET"
17 exit $E_NOARGS # Script needs a command-line argument.
18 elif [ ${#1} -lt $MINLEN ]
19 then
20 echo "Argument must have at least $MINLEN letters."
21 exit $E_BADARG
22 fi
23
24
25
26 FILTER='.......' # Must have at least 7 letters.
27 # 1234567
28 Anagrams=( $(echo $(anagram $1 | grep $FILTER) ) )
29 # $( $( nested command sub. ) )
30 # ( array assignment )
31
32 echo
33 echo "${#Anagrams[*]} 7+ letter anagrams found"
34 echo
35 echo ${Anagrams[0]} # First anagram.
36 echo ${Anagrams[1]} # Second anagram.
37 # Etc.
38
39 # echo "${Anagrams[*]}" # To list all the anagrams in a single line . . .
40
41 # Look ahead to the Arrays chapter for enlightenment on
42 #+ what's going on here.
43
44 # See also the agram.sh script for an exercise in anagram finding.
45
46 exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
></TD
></TR
></TABLE
></DIV
><P
>Examples of command substitution in shell scripts:
<OL
TYPE="1"
><LI
><P
><A
HREF="loops.html#BINGREP"
>Example 11-8</A
></P
></LI
><LI
><P
><A
HREF="testbranch.html#CASECMD"
>Example 11-27</A
></P
></LI
><LI
><P
><A
HREF="randomvar.html#SEEDINGRANDOM"
>Example 9-16</A
></P
></LI
><LI
><P
><A
HREF="moreadv.html#EX57"
>Example 16-3</A
></P
></LI
><LI
><P
><A
HREF="textproc.html#LOWERCASE"
>Example 16-22</A
></P
></LI
><LI
><P
><A
HREF="textproc.html#GRP"
>Example 16-17</A
></P
></LI
><LI
><P
><A
HREF="extmisc.html#EX53"
>Example 16-54</A
></P
></LI
><LI
><P
><A
HREF="loops.html#EX24"
>Example 11-14</A
></P
></LI
><LI
><P
><A
HREF="loops.html#SYMLINKS"
>Example 11-11</A
></P
></LI
><LI
><P
><A
HREF="filearchiv.html#STRIPC"
>Example 16-32</A
></P
></LI
><LI
><P
><A
HREF="redircb.html#REDIR4"
>Example 20-8</A
></P
></LI
><LI
><P
><A
HREF="contributed-scripts.html#TREE"
>Example A-16</A
></P
></LI
><LI
><P
><A
HREF="procref1.html#PIDID"
>Example 29-3</A
></P
></LI
><LI
><P
><A
HREF="mathc.html#MONTHLYPMT"
>Example 16-47</A
></P
></LI
><LI
><P
><A
HREF="mathc.html#BASE"
>Example 16-48</A
></P
></LI
><LI
><P
><A
HREF="mathc.html#ALTBC"
>Example 16-49</A
></P
></LI
></OL
>
</P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN7205"
HREF="commandsub.html#AEN7205"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>For purposes of <I
CLASS="FIRSTTERM"
>command
substitution</I
>, a <B
CLASS="COMMAND"
>command</B
>
may be an external system command, an internal scripting
<A
HREF="internal.html#BUILTINREF"
>builtin</A
>, or even <A
HREF="assortedtips.html#RVT"
>a script function</A
>.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN7211"
HREF="commandsub.html#AEN7211"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>In a more technically correct sense,
<I
CLASS="FIRSTTERM"
>command substitution</I
> extracts the
<TT
CLASS="FILENAME"
>stdout</TT
> of a command, then assigns
it to a variable using the <SPAN
CLASS="TOKEN"
>=</SPAN
>
operator.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN7308"
HREF="commandsub.html#AEN7308"
>[3]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
> In fact, nesting with backticks is also possible,
but only by escaping the inner backticks, as John
Default points out.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 word_count=` wc -w \`echo * | awk '{print $8}'\` `</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
><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="testbranch.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="arithexp.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Testing and Branching</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part3.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Arithmetic Expansion</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|