O'Reilly logo

Unix in a Nutshell, 4th Edition by Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Arithmetic Expressions

The let command performs arithmetic. ksh88 and Bash are restricted to integer arithmetic. ksh93 can do floating-point arithmetic as well. Both shells provide 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.

Operators

The shells use arithmetic operators from the C programming language, in decreasing order of precedence. ksh88 does not support the ++, --, unary +, ?:, comma, or ** operators. Early versions of ksh93 do not have **.

Operator

Description

a In ksh93, the ** operator is right-associative. In bash versions prior to 3.1, it is left-associative. It will be changed to right-associative starting with version 3.1.

++ --

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

+ - ! ~

Unary plus and minus, logical negation and binary inversion (one's complement).

**

Exponentiation.a

* / %

Multiplication; division; modulus (remainder).

+ -

Addition; subtraction.

<< >>

Bitwise left shift; bitwise right shift.

< <= > >=

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

== !=

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.

= += -=

 

*= /= %=

 

<<= >>=

Assignment.

&= ^= |=

 

,

Sequential expression evaluation.

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

hypot

Euclidean distance

acos

Arc cosine

int

Integer part of floating-point number

asin

Arc sine

log

Natural logarithm

atan

Arc tangent

pow

Exponentiation (x y)

atan2

Arc tangent of two values

sin

Sine

cos

Cosine

sinh

Hyperbolic sine

cosh

Hyperbolic cosine

sqrt

Square root

exp

Exponential (e x)

tan

Tangent

fmod

Floating-point remainder

tanh

Hyperbolic tangent

Examples

    let "count=0" "i = i + 1"               Assign i and count
    let "num % 2"                           Test for an even number
    (( percent >= 0 && percent <= 100 ))Test the range of a value

See the let entry in the later section "Built-in Commands (Bash and Korn Shells)" for more information and examples.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required