home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam

#  Chapter 4The Bourne Shell and Korn Shell ## 4.4 Arithmetic Expressions

The Korn shell's let command performs arithmetic. ksh88 is restricted to integer arithmetic. ksh93 can do floating-point arithmetic as well. The Korn shell provides a way to substitute arithmetic values (for use as command arguments or in variables); base conversion is also possible:

 ```\$(( ``` expr ```)) ``` Use the value of the enclosed arithmetic expression. B ```# ``` n Interpret integer n in numeric base B . For example, ```8#100 ``` specifies the octal equivalent of decimal 64.

### 4.4.1 Operators

The Korn shell uses arithmetic operators from the C programming language; in decreasing order of precedence.

Operator

Description

```++ -- ```

Auto-increment and auto-decrement, both prefix and postfix. ksh93 only.

```+ ```

Unary plus. ksh93 only.

```- ```

Unary minus.

```! ~ ```

Logical negation; binary inversion (one's complement).

```* / % ```

Multiplication; division; modulus (remainder).

```+ - ```

```<< >> ```

Bitwise left shift; bitwise right shift.

```<= >= ```

Less than or equal to; greater than or equal to.

```< > ```

Less than; greater than.

```== != ```

Equality; inequality (both evaluated left to right).

```& ```

Bitwise AND.

```^ ```

Bitwise exclusive OR.

```| ```

Bitwise OR.

```&& ```

Logical AND (short-circuit).

```|| ```

Logical OR (short-circuit).

```?: ```

Inline conditional evaluation. ksh93 only.

```*= /= %= ```

Assignment.

```= += -= ```

```<<= >>= ```

```&= ^= |= ```

```, ```

Sequential expression evaluation. ksh93 only.

### 4.4.2 Built-in Mathematical Functions (ksh93 only)

ksh93 provides access to the standard set of mathematical functions. They are called using C function call syntax.

Name Function Name Function
```abs ```

Absolute value

```log ```

Natural logarithm

```acos ```

Arc cosine

```sin ```

Sine

```asin ```

Arc sine

```sinh ```

Hyperbolic sine

```cos ```

Cosine

```sqrt ```

Square root

```cosh ```

Hyperbolic cosine

```tan ```

Tangent

```exp ```

Exponential function

```tanh ```

Hyperbolic tangent

```int ```

Integer part of floating-point number

### 4.4.3 Examples

```let "count=0" "i = i + 1"		Assign i and count   4.3 Variables 4.5 Command History 