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


The expr Command

The expr command performs simple integer arithmetic:

$ expr 8 / 3
2
$

Notice that any fractional result is ignored. The general syntax is

expr integer1 operand integer2

Possible operands are given in Table 18.3.

Table 18.3 expr Operands

Operand Description

+ Addition
- Subtraction
\* Multiplication
/ Integer division (any fraction in the result is dropped)
% Remainder from a division operation (also called the modulus function)

Notice that the * sign must be quoted to prevent shell expansion (see Chapter 9), but the spaces around the * sign must not be quoted:

$ expr 3 \* 5
15
$


The remainder or modulus function is what remains after a division operation:
$ expr 19 % 7
5
$

In this operation, 7 goes into 19 two times with a remainder of 5. The modulus function is often called mod for short. You can say that 19 mod 7 equals 5.

expr requires separate arguments, each separated by a space, or you will not see the calculated result:

$ expr 3+2
3+2
$

expr is often used within backquotes in shell programming to increment a variable:

CNT=`expr $CNT + 1`

expr adds one to the current value in variable CNT, and the backquotes use command substitution to allow the new value to be assigned back to the variable CNT. (See the section “Command and Arithmetic Substitution” in Chapter 8.)

expr can also return the number of characters matched by a regular expression (see Chapter 16):

$ expr $ABC : '.*'
7
$

.* is a regular expression pattern indicating all characters, so all characters of variable $ABC are counted. In this case, expr shows that it contains 7 characters.

$ expr $ABC : '[0-9]*'
4
$

[0-9]* is a regular expression pattern that matches any group of digits. In this example, expr counts the number of the digits that occur at the start of the string. Looking at the previous example, you know that there are four digits at the start of variable ABC.

Because you knew that there were seven characters total in ABC, you now know that the fifth character is not a digit.

If part of the regular expression pattern is grouped in escaped parentheses, expr returns the portion of the pattern indicated by the parentheses:

$ expr abcdef : '..\(..\)..'
cd
$

Each period is a regular expression wildcard that represents one character of the given string. The middle two periods are enclosed in escaped parentheses, so those two characters, cd, are output. This example also illustrates that the string following expr can be a literal string of characters, but it is more common in scripts for the string to be generated by variable or command substitution (see Chapter 8).

The bc Command

bc is an arithmetic utility not limited to integers:

$ bc
scale=4
8/3
2.6666
2.5 * 4.1/6.9
1.4855
quit
$

In this example, you invoke bc and set scale to 4, meaning that you want it to calculate any fraction to four decimal places. You ask it to calculate 8/3, which gives 2.6666 and then a more complex calculation. Note that spaces are optional. Finally you enter quit to return to the shell prompt. bc can handle addition (+), subtraction (-), multiplication (*), division (/), remainder or modulo (%), and integer exponentiation (^). bc can accurately compute numbers of any size:

9238472938742937 * 29384729347298472
271470026887302339647844620892264

bc can be used in shell variable assignment to assign calculated values to variables:

AVERAGE='echo "scale=4; $PRICE/$UNITS" | bc'

The echo command is used here to print directives that are piped to bc. The first directive sets the scale to 4; the second directive is a division operation. These directives are piped to bc, which does the calculations and returns the result. The backquotes allow the result from bc to be stored in the variable AVERAGE.

bc allows conversion between different number bases:

$ bc
obase=16
ibase=8
400
100
77
3f
10*3
18
quit
$

obase=16 sets the output base to hexadecimal; ibase=8 sets the input base to octal. It is important to set the output base first. You enter 400. It shows an octal 400 is a hex 100. You enter 77. It shows an octal 77 is a hex 3f. Then you multiply 10 and 3, which equals 24 because 10 octal is 8 and 8*3 is 24. However, because the output base is hex, bc converts 24 to hex, which gives 18 as the reported result.


Previous Table of Contents Next