UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: 4.3 VariablesChapter 4
The Bourne Shell and Korn Shell
Next: 4.5 Command History
 

4.4 Arithmetic Expressions

The Korn shell's let command performs integer arithmetic. The Korn shell provides a way to substitute integer 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; they are listed below in decreasing order of precedence:

-

Unary minus.

! ~

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

* / %

Multiplication; division; modulus (remainder).

+ -

Addition; subtraction.

<< >>

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.

||

Logical OR.

*= /= %=

Assignment.

= += -=

<<= >>=

&= ^= |=

4.4.2 Examples

See the let command for more information and 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.


Previous: 4.3 VariablesUNIX in a Nutshell: System V EditionNext: 4.5 Command History
4.3 VariablesBook Index4.5 Command History

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System