Previous Table of Contents Next

Module 73
let (ksh) / @ (csh)


The internal let command evaluates arithmetic expressions. let performs all arithmetic calculations in long integer. No overflow checking is performed on the results.

C Shell
The C shell provides arithmetic evaluation similar to the let command. The command line must begin with @ ("at sign").


Following is the general format of the let command.

    let [ expression ... ]
    (( expression ))
(( )) The double left parentheses begin the evaluation of an expression. The double right parentheses end the evaluation. This notation is equivalent to the let "expression" command. Double quotes are NOT required to prevent shell interpretation of the *, < and > operators.

C Shell
@ var = expression
@ var[n] = expression


The following list describes the expressions that may be passed to the let command.

expression An arithmetic expression consisting of constants, variables, and operators. The constants and variables must be numbers. Valid formats of expression with let format are:
let lcv=lcv+1 # no spaces allowed in expression
let "lcv = lcv + 1" # spaces in expression, must use double quotes
Multiple expressions may be placed on the command line.
Variable A shell variable. Assigned the result of the expression or provides a number for the expression. You do not have to precede the variables used in an expression with a dollar sign ($).
Constant A number or a shell variable containing a number. The variable does not have to be an integer attribute type (see typeset command). The constant is written as base#number. The base is a number between 2 and 36. If no base is given, base 10 is used. Base eight numbers can be represented with a leading 0 (0number) and base sixteen numbers with a leading 0x (0xnumber).
Operator An arithmetic operator, such as +, -, *, and /. See section on Operators.
( ) Parentheses may be used inside an expression to override the precedence of operators. Basically used to group certain operators with constants to control the order of evaluation.

C Shell
@ var = expression With no arguments, the values for all shell variables are displayed.
@var[n] = expression With arguments, var or the nth word of var is set to the value of expression. If var[n] is used, it must already exist.
@ cnt = $num + 1 # No quotes needed unless the <, >,
# &, or | characters are used.
The double quotes prevent the shell from interpreting the *, < and > operators. If you use any of these operators, you must enclose the expression in double quotes.
The @ command does not support different base numbers. It uses only base 10.


let evaluates the operators in the following order. If two or more operators are in the same expression, then let evaluates them left to right on the command line.

() Override normal precedence rules.
- Unary minus
! Logical negation (not)
~ Bitwise negation
* / % Multiplication, division, remainder. The remainder is that of the first expression after dividing it by the second expression.
+ - Addition and subtraction
<< >> Left shift and right shift. Bit shift the first expression by the number of bits specified in the second expression.
<= >= < > Comparison; less than or equal to, greater than or equal to, less than, greater than
== != Equal and not equal
& Bitwise and. The value contains a 1 in each bit position if both expressions have a 1 in the corresponding bit position. Contains a 0 if either expression has a 0.
^ Bitwise exclusive or. Value contains a 1 in each bit position if only one expression has a 1 in the corresponding bit position. If both expressions have a 1 or a 0, then the value contains a 0 in the corresponding bit position.
| Bitwise or. The value contains a 1 in each bit position if the corresponding bit of either expression contains a 1. If both expressions contain 0 in corresponding bit positions, then the value contains 0.
&& logical and. If the first expression evaluates to 0, the second expression is not evaluated.
|| Logical or. If the first expression evaluates to nonzero, the second expression is not evaluated.
= assignment
op= Compound assignment. This is the same as Variable=Variable op expression, where op is *, /, %, +, -, <<, >>, &, ^, or |. For example
((CNT += 10))
is equivalent to
((CNT = CNT + 1))

Previous Table of Contents Next