O'Reilly logo

Mac OS X Tiger in a Nutshell by Jason McIntosh, Chuck Toporek, Chris Stone, Andy Lester

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 integer arithmetic. bash provides a way to substitute integer values (for use as command arguments or in variables); base conversion is also possible.

Table 5-30. Syntax for arithmetic expressions

Expression

Meaning

((expr))

Use the value of the enclosed arithmetic expression.

Operators

bash uses arithmetic operators from the C programming language; the following list is in decreasing order of precedence. Use parentheses to override precedence.

Table 5-31. Arithmetic operators

Operator

Meaning

-

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

=

Assign value

+= -=

Reassign after addition/subtraction

*= /= %=

Reassign after multiplication/division/remainder

&= ^= |=

Reassign after bitwise AND/XOR/OR

<<= >>=

Reassign after bitwise shift left/right

Examples

See the let built-in command for more information and examples.

    let "count=0" "i = i + 1"     Assign i and count
    let "num % 2"; echo $?         Test for an even number

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