Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Command and Arithmetic Substitution

Two additional forms of substitution provided by the shell are

  Command substitution
  Arithmetic substitution


Command substitution enables you to capture the output of a command and substitute it in another command, whereas arithmetic substitution enables you to perform simple integer mathematics using the shell.

Command Substitution

Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the place of the commands. Command substitution is performed when a command is given as

`command`

Here command, can be a simple command, a pipeline, or a list.


Caution:  
Make sure that you are using the backquote, not the single quote character, when performing command substitution. Command substitution is performed by the shell only when the backquote, or backtick, character, ', is given. Using the single quote instead of the back quote is a common error in shell scripts leading to many hard to find bugs.

Command substitution is generally used to assign the output of a command to a variable. Each of the following examples demonstrate command substitution:

DATE=`date`
USERS=`who | wc –l`
UP=`date ; uptime`

In the first example, the output of the date command becomes the value for the variable DATE. In the second example, the output of the pipeline becomes the value of the variable USERS. In the last example, the output of the list becomes the value of the variable UP.

You can also use command substitution to provide arguments for other commands. For example,

grep `id –un` /etc/passwd

looks through the file /etc/passwd for the output of the command:

id –un

In my case, the command substitution results in the string ranga, and thus grep returns the entry in the passwd file for my user name.

Arithmetic Substitution

In ksh and bash, the shell enables integer arithmetic to be performed. This avoids having to run an extra program such as expr or bc to do math in a shell script. This feature is not available in sh.

Arithmetic substitution is performed when the following form of command is given:

$((expression))

Expressions are evaluated according to standard mathematical conventions. Table 8.3 provides the available operators. The operators are listed in order of precedence.

Table 8.3 Arithmetic Substitution Operators

Operator Description

/ The division operator. Divides two numbers and returns the result.
* The multiplication operator. Multiples two numbers and returns the result.
- The subtraction operator. Subtracts two numbers and returns the result.
+ The addition operator. Adds two numbers and returns the result.
() The parentheses clarify which expressions should be evaluated before others.

You use the following command as an illustration of the operators and their precedence:

foo=$(( ((5 + 3*2) - 4) / 2 ))

After this command executes the value of foo to 3. Because this is integer arithmetic, the value is not 3.5, and because of operator precedence the value is not 6.

Summary

In this chapter, you have looked at the four main forms of substitution available in the shell:

  Filename substitution
  Variable substitution
  Command substitution
  Arithmetic substitution

As you write scripts and use the shell to solve problems, you find that these types of substitution help you extensively.

Questions

1.  What combination of wildcards should you use to list all the files in the current directory that end in the form hwXYZ.ABC?
Here X and Y can be any number; Z is a number between 2 and 6; and A, B, and C are any character.
2.  What action is performed by the following line, if the variable MYPATH is unset:
: ${MYPATH:=/usr/bin:/usr/sbin:/usr/ucb}
3.  What is the difference between the actions performed by the command given in the previous problem and the action performed by the following command:
: ${MYPATH:-/usr/bin:/usr/sbin:/usr/ucb}
4.  What is the result of the following arithmetic substitution:
$(( 3 * 2 + ( 4 – 3 / 4) ))


Previous Table of Contents Next