Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 21 Basic Shell Programming

Arithmetic Evaluation Using let

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

let, another command unique to POSIX and Korn Shell, enables shell scripts to use arithmetic expressions. This command allows long integer arithmetic.

The syntax is:

let arg...

where each arg is an arithmetic expression of shell parameters and operators to be evaluated by the shell. Table 21-2 “Operator Decreasing Precedence Order” lists the operators in decreasing order of precedence.

Table 21-2 Operator Decreasing Precedence Order

Operator

Description

-

unary minus

!

logical negation

* / %

multiplication, division, remainder

+ -

addition, subtraction

<= >= < >

comparison

== !=

equals, does not equal

=

assignment

 

In the next example, x is set to 1, and then when the let command executes:

  • first, 1*6 is evaluated to 6,

  • then 3/1 is evaluated to 3,

  • then x is added to the 6 which equals 7, and

  • finally the 3 is subtracted from the 7 to equal 4.

$ x=1
$ let x=x+1*6-3/1
$ echo $x
4

You can also use parenthesis to create this effect, or override the operator's precedence to produce different results, 9. (When using parenthesis, double quotes are necessary.)

$ let "x=x+(1*6)-(3/1)"
$ let "x=(x+1)*6-3/1"

This next script reads a value from the user, compares it to 14, and prints an appropriate string based on the comparison:

$ read x
$ y=14
$ if (( x >= y ))
> then echo greater or equal
> else echo less than
> fi

Using (( )) around the expression replaces using the let:

let "x >= y"

(which must be quoted to allow blanks and prevent the > from being interpreted as an I/O redirection). Also, you do not need to put $ in front of x or y. In this situation, the let command is used as a condition.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.