Previous Section  < Day Day Up >  Next Section

9.11. Command Substitution

A string or variable can be assigned the output of a UNIX command by placing the command in backquotes. This is called command substitution. (On the keyboard, the backquote is normally below the tilde character.) If the output of a command is assigned to a variable, it is stored as a wordlist (see "Arrays" on page 447), not a string, so that each of the words in the list can be accessed separately. To access a word from the list, a subscript is appended to the variable name. Subscripts start at 1.

Example 9.60.

1   % echo The name of my machine is `uname -n`.

    The name of my machine is stardust.

2   % echo The present working directory is `pwd`.

    The present working directory is /home/stardust/john.

3   % set d = `date`

    % echo $d

    Sat Jun 20 14:24:21 PDT 2004

4   % echo $d[2] $d[6]

    Jun 2004

5   % set d = "'`date`"

    % echo $d[1]

    Sat Jun 20 14:24:21 PDT 2004


EXPLANATION

  1. The UNIX command uname –n is enclosed in backquotes. When the shell encounters the backquotes, it will execute the enclosed command, uname –n, and substitute the output of the command, stardust, into the string. When the echo command prints its arguments to standard output, the name of the machine will be one of its arguments.

  2. The UNIX command pwd is executed by the shell and the output is substituted in place within the string.

  3. The local variable d is assigned the output of the date command. The output is stored as a list of words (an array).

  4. Elements 2 and 6 of the d array are printed. The subscripts start at 1.

  5. Because the output is enclosed in double quotes, it is a single string rather than a wordlist.

9.11.1 Wordlists and Command Substitution

When a command is enclosed in backquotes and assigned to a variable, the resulting value is an array (wordlist). Each element of the array can be accessed by appending a subscript to the array name. The subscripts start at 1. If a subscript that is greater than the number of words in the array is used, the C shell prints Subscript out of range. If the output of a command consists of more than one line, the newlines are stripped from each line and replaced with a single space.

Example 9.61.

1   % set d = `date`

    % echo $d

    Fri Aug 27 14:04:49 PDT 2004

2   % echo $d[1–3]

    Fri Aug 27

3   % echo $d[6]

    2004

4   % echo $d[7]

    Subscript out of range.

5   % echo "The calendar for the month of November is `cal 11 2004`"

    The calendar for month of November is November 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


EXPLANATION

  1. The variable d is assigned the output of the UNIX date command. The output is stored as an array. The value of the variable is displayed.

  2. The first three elements of the array are displayed.

  3. The sixth element of the array is displayed.

  4. There are not seven elements in the array. The shell reports that the subscript is out of range.

  5. The output spans more than one line. Each newline is replaced with a space. This may not be the output you expected.

Example 9.62.

1   % set machine = `rusers | awk '/tom/{print $1}'`

2   % echo $machine

    dumbo bambi dolphin

3   % echo $#machine

    3

4   % echo $machine[$#machine]

    dolphin

5   % echo $machine

    dumbo bambi dolphin

6   % shift $machine

    % echo $machine

    bambi dolphin

7   % echo $machine[1]

    bambi

8   % echo $#machine

    2


EXPLANATION

  1. The output of the rusers command is piped to awk. If the regular expression tom is found, awk prints the first field. The first field, in this case, is the name of the machine(s) where user tom is logged on.

  2. User tom is logged on three machines. The names of the machines are displayed.

  3. The number of elements in the array is accessed by preceding the array name with $#. There are three elements in the array.

  4. The last element of the array is displayed. The number of elements in the array ($#machine) is used as a subscript.

  5. The array is displayed.

  6. The shift command shifts the array to the left. The first element of the array is dropped and the subscripts are renumbered, starting at 1.

  7. The first element of the array after the shift is displayed.

  8. After the shift, the length of the array has decreased by one.

    Previous Section  < Day Day Up >  Next Section