The let Command

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 6.  Performing Arithmetic


The let Command

Integer arithmetic can be done with the let command and arithmetic expressions. The format for the let command is:

 let "arithmetic-expression" 

where arithmetic-expressions can contain constants, operators, and Korn shell variables. Double quotes are used with arithmetic expressions that contain white space or operators that have special meaning to the Korn shell. For example, variable X can be set to the sum of 1+1 like this:

 $ let "X=1 + 1"  $ print $X  2 

then incremented:

 $ let "X=X + 1"  $ print $X  3 

Notice that in arithmetic expressions, regular variables can be referenced by name only, and do not have to be preceded by $ for substitution to be performed. Both

 $ let "X=X + 1" 

and

 $ let "X=$X + 1" 

are equivalent. The first format is preferred because parameter expansion does not have to be performed. This causes it to be executed faster.

Table 6.1. Arithmetic Operators (in order of precedence)

?/P>

unary minus

!

logical negation

~

bitwise negation

*, /, %

multiplication, division, remainder (modulo)

+, ?/P>

addition, subtraction

<<, >>

left shift, right shift

<=, <

less than or equal to, less than

>=, >

greater than or equal to, greater than

==

equal to

!=

not equal to

&

bitwise AND

^

bitwise exclusive OR

|

bitwise OR

&&

logical AND

||

logical OR

=

assignment

*=, /=, %=

multiply assign, divide assign, modulo assign

+=, ?

increment, decrement

<<=, >>=

left shift assign, right shift assign

&=, ^=, |=

bitwise AND assign, bitwise exclusive OR assign, bitwise OR assign

(...)

grouping (used to override precedence rules)

Arithmetic expressions are made up of constants, variables or any of the arithmetic operators in Table 6.1.


       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net