Previous Section  < Day Day Up >  Next Section

14.3. Arithmetic

14.3.1 Integers (declare and let Commands)

The declare Command

Variables can be declared as integers with the declare –i command. If you attempt to assign any string value, bash assigns 0 to the variable. Arithmetic can be performed on variables that have been declared as integers. (If the variable has not been declared as an integer, the built-in let command allows arithmetic operations. See "The let Command" on page 873.) If you attempt to assign a floating-point number, bash reports a syntax error . Numbers can also be represented in different bases such as binary, octal, and hex.

Example 14.6.

1   $ declare –i num

2   $ num=hello

    $ echo $num

    0

3   $ num=5 + 5

    bash: +: command not found

4   $ num=5+5

    $ echo $num

    10

5   $ num=4*6

    $ echo $num

    24

6   $ num="4 * 6"

    $ echo $num

    24

7   $ num=6.5

    bash: num: 6.5: syntax error in expression (remainder of expression is ".5")


EXPLANATION

  1. The declare command with the –i option creates an integer variable num.

  2. Trying to assign the string hello to the integer variable num causes the string to be stored as 0.

  3. The whitespace must be quoted or removed unless the let command is used.

  4. The whitespace is removed and arithmetic is performed.

  5. Multiplication is performed and the result assigned to num.

  6. The whitespace is quoted so that the multiplication can be performed and to keep the shell from expanding the wildcard (*).

  7. Because the variable is set to integer, adding a fractional part causes a bash syntax error.

Listing Integers

The declare command with only the –i argument will list all preset integers and their values, as shown in the following display.


$ declare –i

declare -ir EU     # effective user id

declare -ir PP    # parent process id

declare -ir U      # user id


Representing and Using Different Bases

Numbers can be represented in decimal (base 10, the default), octal (base 8), hexadecimal (base 16), and a range from base 2 to 36.

FORMAT


variable=base#number-in-that-base


Example 14.7.

n=2#101     # Base is 2; number 101 is in base 2


Example 14.8.

(The Command Line)

1  $ declare -i x=017

   $ echo $x

   15

2  $ x=2#101

   $ echo $x

   5

3  $ x=8#17

   $ echo $x

   15

4  $ x=16#b

   $ echo $x

   11


EXPLANATION

  1. The declare function is used to assign an integer variable x the octal value 017. Octal numbers must start with a leading 0. 15, the decimal value of 017, is printed.

  2. The variable x is assigned the value of 101 (binary). 2 represents the base, separated by a #, and the number in that base, 101. The value of x is printed as decimal, 5.

  3. The variable x is assigned the value of 17 (octal). The value of x is printed as decimal, 15.

  4. The variable x is assigned the value of b (hexadecimal). The value of x is decimal 11.

The let Command

The let command is a bash built-in command that is used to perform integer arithmetic and numeric expression testing. To see what let operators your version of bash supports, type at the prompt:


help let


A list of the let operators is also found in Table 14.4 on page 884.

Example 14.9.

(The Command Line)

1   $ i=5  or let i=5

2   $ let i=i+1

    $ echo $i

    6

3   $ let "i = i + 2"

    $ echo $i

    8

4   $ let "i+=1"

    $ echo $i

    9

5   $ i=3

6   $ (( i+=4))

    $ echo $i

    7

7   $ (( i=i-2 ))

    $ echo $i

    5


Table 14.4. The let Command Operators[a]

Operator

Meaning

Unary minus

+

Unary plus

!

Logical NOT

~

Bitwise NOT (negation)

*

Multiply

/

Divide

%

Remainder

+

Add

Subtract

let Operators Not Implemented Prior to bash 2.x

<<

Bitwise left shift

>>

Bitwise right shift

<= >= < >

Comparison operators

== !=

Equal to and not equal to

&

Bitwise AND

^

Bitwise exclusive OR

|

Bitwise OR

&&

Logical AND

||

Logical OR

= *= /= %= += –= <<= >>= &= ^= |=

Assignment and shortcut assignment


[a] See Example 14.18.

EXPLANATION

  1. The variable i is assigned the value 5.

  2. The let command will add 1 to the value of i. The $ (dollar sign) is not required for variable substitution when performing arithmetic.

  3. The quotes are needed if the arguments contain whitespace.

  4. The shortcut operator, +=, is used to add 1 to the value of i.

  5. The variable i is assigned the value 3.

  6. The double parentheses can be used to replace let.[a] 4 is added and assigned to i.

    [a] Implemented in versions of bash 2.x.

  7. 2 is subtracted from i. We could have also written i–=2

14.3.2 Floating-Point Arithmetic

Bash supports only integer arithmetic, but the bc, awk, and nawk utilities are useful if you need to perform more complex calculations.

Example 14.10.

(The Command Line)

1   $ n=`echo "scale=3; 13 / 2" | bc`

    $ echo $n

    6.500

2   product=`gawk -v x=2.45 -v y=3.123 'BEGIN{printf "%.2f\n",x*y}'`

    $ echo $product

    7.65


EXPLANATION

  1. The output of the echo command is piped to the bc program. The scale is set to 3, which is the number of significant digits to the right of the decimal point that will be printed. The calculation is to divide 13 by 2. The entire pipeline is enclosed in backquotes. Command substitution will be performed and the output assigned to the variable n.

  2. The gawk program gets its values from the argument list passed in at the command line, x=2.45 y=3.123. After the numbers are multiplied, the printf function formats and prints the result with a precision of two places to the right of the decimal point. The output is assigned to the variable product.

    Previous Section  < Day Day Up >  Next Section