Previous Section  < Day Day Up >  Next Section

13.12. Command Substitution

Command substitution is used when assigning the output of a command to a variable or when substituting the output of a command within a string. All shells use backquotes to perform command substitution.[12] Bash allows two forms: the older form, where the command(s) is placed within backquotes, and the new Korn-style form, where the command(s) is placed within a set of parentheses preceded by a dollar sign.

[12] The bash shell allows backquotes for command substitution for upward compatibility, but provides an alternate method as well.

Bash performs the expansion by executing the command and returning the standard output of the command, with any trailing newlines deleted. When the old-style backquote form of substitution is used, the backslash retains its literal meaning except when followed by $, `, or \. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

Command substitutions may be nested. To nest when using the old form, the inner backquotes must be escaped with backslashes.

FORMAT


`UNIX command`   # Old method with backquotes



$(UNIX command)  # New method


Example 13.75.

(The Old Way)

1   $ echo "The hour is `date +%H`"

    The hour is 09

2   $ name=`awk –F: '{print $1}' database`

    $ echo $name

    Ebenezer Scrooge

3   $ ls `ls /etc`

    shutdown

4   $ set `date`

5   $ echo $*

    Wed Jul 14 09:35:21 PDT 2004

6   $ echo $2 $6

    Jul 2004

7   $ echo `basename \`pwd\``

    ellie


EXPLANATION

  1. The output of the date command is substituted into the string.

  2. The output of the awk command is assigned to the variable name and displayed.

  3. The output of the ls command, enclosed in backquotes, is a list of files from the /etc directory. The filenames will be arguments to the first ls command. All files with the same name in /etc in the current directory are listed. (The files that are not matches in this directory will cause an error message, such as ls: termcap: No such file or directory.)

  4. The set command assigns the output of the date command to positional parameters. Whitespace separates the list of words into its respective parameters.

  5. The $* variable holds all of the parameters. The output of the date command was stored in the $* variable. Each parameter is separated by whitespace.

  6. The second and sixth parameters are printed.

  7. To set the variable dirname to the name (only) of the present working directory, command substitution is nested. The pwd command is executed first, passing the full pathname of the present working directory as an argument to the UNIX command basename. The basename command strips off all but the last element of a pathname. When nesting commands within backquotes, the backquotes for the inner command must be escaped with a backslash.

The bash alternate for using backquotes in command substitution is presented below in Example 13.76.

Example 13.76.

(The New Way)

1   $ d=$(date)

    $ echo $d

     Wed Jul 14 09:35:21 PDT 2004

2   $ lines = $(cat filex)

3   $ echo The time is $(date +%H)

    The time is 09

4   $ machine=$(uname –n)

    $ echo $machine

    jody

5   $ pwd

    /usr/local/bin

    $ dirname="$(basename $(pwd)) "   # Nesting commands

    $ echo $dirname

    bin

6   $ echo $(cal)                           # Newlines are lost

    July 2004 S M Tu W Th F S 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

7  $ echo "$(cal)"

        July 2004

    S  M  Tu  W  Th  F  S

                  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


EXPLANATION

  1. The date command is enclosed within parentheses. The output of the command is substituted into the expression, then assigned to the variable d, and displayed.

  2. The output from the cat command is assigned to the variable lines.

  3. Again the date command is enclosed in parentheses. The output of date +%H, the current hour, is substituted for the expression and echoed to the screen.

  4. The variable machine is assigned the output of uname –n, the name of the host machine. The value of the machine variable is echoed to the screen.

  5. The output of the pwd command (present working directory) is /usr/local/bin. The variable dirname is assigned the output resulting from command substitution where the command substitutions are nested. $(pwd) is the first command substitution to be be performed. The output of the pwd command is substituted in the expression, then the basename program will use the results of that substitution, /usr/local/bin, as its argument, resulting in basename /usr/local/bin.

  6. The output of the cal (the current month) command is echoed. The trailing newlines are deleted when command substitution is performed.

  7. When you put the whole command substitution expression in double quotes, the trailing newlines are preserved, and calendar looks like it should.

    Previous Section  < Day Day Up >  Next Section