Introduction
Bash let
is a built-in command in Linux systems used for evaluating arithmetic expressions. Unlike other arithmetic evaluation and expansion commands, let
is a simple command with its own environment. The let
command also allows for arithmetic expansion.
In this tutorial, we will go over the let command syntax, options, and usage examples.
Prerequisites
- A system running a Linux distribution
- Access to the terminal window or command line
Bash let Statement Syntax
The let
command uses the following basic syntax:
let [expression]
In the syntax above, expression
is an arithmetic expression you want the let
command to evaluate. let
only works with expressions that contain integers. The command does not support floating-point numerals.
Note: When using the let
command, it is best to enclose individual expressions in double quotation marks ( " ) to avoid any errors.
The let
command allows users to evaluate more than one expression simultaneously. In this case, the syntax is:
let [expression 1] [expression 2] … [expression 3]
let Arithmetic Operators
The Bash let
command is able to evaluate expressions that contain the arithmetic operators from the table below. The entries are listed in the order of decreasing precedence, with the operators of the same precedence listed in the same row:
Operator | Description |
var++ var-- | Post-increment (++): Interpret the value of a variable and add 1 to it. Post-decrement (--): Interpret the value of a variable and subtract 1 from it. |
++var --var | Pre-increment (++): Add 1 to the value of a variable and then interpret the value. Pre-decrement (--): Subtract 1 from the value of a variable and then interpret the value. |
-expr +expr | Unary minus: Return the value of the expression multiplied by -1. Unary plus: Return the value of the expression unchanged (multiplied by 1). |
! ~ | Logical negation: Return false if the operand is true and true if the operand is false. Bitwise negation: Flip the bits in the binary representation of a number. |
** | Exponentiation: Raise one integer to the power of another. |
* / % | Basic arithmetic operators: Multiplication. Division. Remainder (modulo). |
+ - | Basic arithmetic operators: Addition. Subtraction. |
<< >> | Bitwise shift left. Bitwise shift right. |
<= >= < > | Value comparison: Less than or equal to. Greater than or equal to. Less than. Greater than. |
== != | Equality: Returns true if the operands are equal. Inequality: Returns false if the operands are equal. |
& | Bitwise AND: Multiplies the corresponding digits of two binary values. |
^ | Bitwise XOR: Compares the corresponding digits of two binary values and returns 1 if the digits differ. |
| | Bitwise OR: Compares the corresponding digits of two binary values and returns 1 if either of the digits is a 1. |
&& | Logical AND: Returns true if both operands are true. |
|| | Logical OR: Returns true if either of the operands is true. |
expr1 ? expr2 : expr3 | Conditional operator: If expr1 is true, return expr2. If expr1 is false, return expr3. |
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= | Assignment: Assign a value to a variable. |
Bash let Examples
The let
command lets you assign values to variables and perform arithmetic, bitwise, and logical operations. Read the sections below for examples.
Assigning a Value to a Variable
Use the let
command to assign a value to a variable with:
let "[variable] = [value]"
Note: Learn more about assigning values to variables in our guide to environment variables in Linux.
For instance, assign the value of 5 to the variable var1 and print out the value as the output:
let "var1 = 5"; echo $var1
Likewise, after setting a variable value, the let
command lets you assign the same value to another variable:
let "var1 = 5" "var2=var1"; echo $var2
Performing Basic Arithmetic Operations
After setting variable values, use the let
command to perform basic arithmetic operations. For example, set var1 as 6 and var2 as 2 and perform addition, subtraction, multiplication, division, and modulus:
let "var1 = 6" "var2 = 2" "var3=var1+var2"; echo $var3
let "var1 = 6" "var2 = 2" "var3=var1-var2"; echo $var3
let "var1 = 6" "var2 = 2" "var3=var1*var2"; echo $var3
let "var1 = 6" "var2 = 2" "var3=var1/var2"; echo $var3
let "var1 = 6" "var2 = 2" "var3=var1%var2"; echo $var3
In this example, the let
command sets the value of var3 as the result of the arithmetic operation and prints out the result.
Performing Exponentiation
The let
command requires two integer values to perform exponentiation. The first value (base
) is raised to the power of the second value (power
):
let "[base] ** [power]"
For example, to raise the base value of 3 to the power of 2 and print the result, use this command:
let "var1 = 3 ** 2"; echo $var1
Performing Unary Operations
Use a unary minus with the let
command to change an expression from positive to negative and vice versa:
let "var1 = 2 + 3" "var1=-var1"; echo $var1
The unary plus multiplies the expression by 1 and returns an unchanged value:
let "var1 = 2 + 3" "var1=+var1"; echo $var1
Changing the Variable Value Incrementally
Using the post-increment or post-decrement operators interprets the variable value and then increases or decreases it by 1:
let "var1 = 10" "var2=var1++"; echo $var1 $var2
In the example above, the variable var2 gets the value of var1 (10) before it is increased by 1. The same happens when performing a post-decrement:
let "var1 = 10" "var2=var1--"; echo $var1 $var2
Performing a pre-increment increases the value of var1 by 1 (11), then assigns the new value to var2:
let "var1 = 10" "var2=++var1"; echo $var1 $var2
Performing a pre-decrement does the same. However, this time it decreases the value of var1 by 1 (9):
let "var1 = 10" "var2=--var1"; echo $var1 $var2
Performing Bit Shifts
Performing bit shifts using the let
command shifts the order of the digits in a binary number to the left or right. For instance, performing a left bit shift of the number 8 twice multiplies it by two twice:
let "var1 = 8 << 2"; echo $var1
Conversely, performing a right bit shift twice divides the value by 2 twice:
let "var1 = 8 >> 2"; echo $var1
Performing Bitwise Operations
A bitwise negation reverses each digit of a binary number. This operation changes 1 into 0 and vice versa:
let "var1 = 5" "var2=~var1"; echo $var2
In the example above, the let command converts the value of 5 to a 32-bit signed integer (00000000000000000000000000000101) and reverses the digits. This operation results in -6 (11111111111111111111111111111010).
A bitwise AND operator converts a pair of values into 32-bit signed integers and compares the digits in order. The action returns 1 if both digits are 1, or else it returns 0:
let "var1 = 5" "var2 = 10" "var3 = var1&var2"; echo $var3
The bitwise OR operator compares the corresponding digits of two values and returns 1. But, at least one of the bits must be 1:
let "var1 = 5" "var2 = 10" "var3 = var1|var2"; echo $var3
The bitwise XOR operator compares the corresponding digits of two values and returns 0 if the digits match. Otherwise, the operator returns 1:
let "var1 = 5" "var2 = 10" "var3 = var1^var2"; echo $var3
Conclusion
After reading this tutorial, you should know how to use the Bash let
command to evaluate arithmetic expressions in the Bash shell.
Learn more about using the Bash shell in our guide to writing and executing Bash scripts.