let command (evaluates the values of arithmetic expressions)
- Organization of this page
Format 1
let arithmetic-expression[,arithmetic-expression ... ]
Format 2
((arithmetic-expression))
Description
This command performs numeric calculations in order to evaluate specified arithmetic expressions.
In addition to using the let command, you can also calculate the result of an arithmetic expression using the syntax ((arithmetic-expression)), and the results will be evaluated in the same way as for the let command.
The let command accepts multiple arithmetic expressions delimited by the comma (,). If you specify more than one arithmetic expression, they are calculated in order from left to right. As a result, if you use a conditional expression to evaluate a comma-separated list of arithmetic expressions, a conditional is evaluated taking into account the result of the arithmetic expression that was executed last. Note that specifying any spaces before or after a delimiter comma results in termination of the command with an arithmetic error. You can group calculations in parentheses in order to change the priority of operations.
For details about arithmetic expressions, see 5.3 Arithmetic operations. For details about conditionals, see 5.2 Conditionals.
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination
|
1 |
Normal termination
|
Error termination
|
|
2 |
Error termination
|
Notes
-
Even if the command syntax of this regular built-in command is invalid, it does not exit the shell that is executing the command.
-
The arithmetic operators *, &, <, <<, >, and >> have special meanings as metacharacters. To use these characters in the let command, you must disable them as metacharacters.
Example: Set the variable RC to 1 shifted 2 bits to the left.
Contents of the job definition script
let "RC=1<<2" echo $RC
Contents of the STDOUT file of the execution job
******** JOB SCOPE STDOUT ******** 4
-
The let command accepts no options. If you specify -alphabetic-character as an argument, it will be interpreted as a variable name, not an option.
In the example shown below, -a is specified as the argument. It is interpreted as -3, with a return code of 0.
Contents of the job definition script
a=3 let -a echo $?
Contents of the STDOUT file of the execution job
******** JOB SCOPE STDOUT ******** 0
-
When executing the let command in a separate process, such as the & operator and a command substitution, also see the notes provided in 5.1.7 Execution in a separate process (UNIX only).
Usage examples
-
Add 3+4 and multiply the result by 2.
Contents of the job definition script
let "VAR=2*(3+4)" echo $VAR
Contents of the STDOUT file of the execution job
******** JOB SCOPE STDOUT ******** 14
-
Set the variable RC to the result of 1+2.
Contents of the job definition script
((RC=1+2)) echo $RC
Contents of the STDOUT file of the execution job
******** JOB SCOPE STDOUT ******** 3