FUNDAMENTALS A Complete Guide for Beginners
The let command is a built-in command in Linux that you can use to evaluate arithmetic expressions. You can use different arithmetic operators like +,-,*,/ etc., as well as bitwise operators like AND, OR, XOR, and so on, with the let command. This provides greater flexibility to construct complex arithmetic expressions and conveniently store the result in a variable.
In this article, I’ll demonstrate the most common applications of the let command in Linux with 5 examples.
Syntax of “let” Command
The syntax of the let command in Linux is:
let arg [arg ...]
Here, each arg is a separate “arithmetic expression” to be evaluated.
Note: The arg inside the square brackets refers to optional arguments. Three dots after this arg indicate that multiple values can be passed to the argument.
Arithmetic Operators to Use With “let” Command
The let command can accept a wide range of operators. To check out all the available operators, you can browse the help page for the let command:
help let
The most useful operators from the help page include:
Operator | Description |
---|---|
id++, id– | Variable post-increment, post-decrement. Interprets the value of id variable and adds or subtracts 1 from it. |
++id, –id | Variable pre-increment, pre-decrement. Adds or subtracts 1 from the variable and interprets its new value. |
-expr, +expr | Unary minus, plus. Multiply the value of the expression by -1 or +1. |
+, – | Used for basic arithmetic operations like addition, and subtraction. |
** | Exponentiation. Calculates the exponent of two integers. |
<<, >> | Left and Right Bitwise shifts. |
& | Bitwise AND. |
^ | Bitwise XOR. |
| | Bitwise OR. |
5 Examples of Using “let” Command in Linux
You can use the let command to perform different arithmetic evaluations. Let’s explore the use of the let command with relevant examples:
1. Using “let” Command to Perform Arithmetic Operations
Basic arithmetic operations like addition, subtraction, multiplication, etc. can be evaluated using the let command. Let’s assign 2 numbers to two variables, evaluate the arithmetic operations, and display the result by typing the following commands in the terminal:
let "num1 = 9" "num2 = 5" "num3=num1+num2"; echo $num3
let "num1 = 9" "num2 = 5" "num3=num1-num2"; echo $num3
let "num1 = 9" "num2 = 5" "num3=num1*num2"; echo $num3
let "num1 = 9" "num2 = 5" "num3=num1/num2"; echo $num3
let "num1 = 9" "num2 = 5" "num3=num1%num2"; echo $num3
These command lines will assign 9 and 5 to the num1 and num2 variables, perform arithmetic operations, and store the result in the num3 variable. Finally, it will display the value of the num3 variable.
In the above image, you can see, the use of the let command in common arithmetic operations.
Note: It is a good practice to use double quotes for each expression and use a semicolon at the end of an arithmetic operation.
2. Unary Minus and Unary Plus Operations Using “let” Command
To multiply a given expression by -1 or +1, you can use the unary minus (-expr) or unary plus (+expr) operators, respectively. The unary minus operation alters the sign of a value, that is, it makes the positive value negative and vice versa. For example, consider the following command line:
let "num1 = 9" "num1 = -num1"; echo $num1
In the above image, you can see the use of the unary minus operator to change a positive expression to a negative one.
Note: The unary plus operation keeps the value intact. Sometimes it is useful to increase the readability of a code, especially if you are working with a long bash script and cases like that.
3. Using “let” Command to Use Exponent (**) Operator
To get the value of a number, raised to the value of another number, you can use the exponent operator with the let command. For example, to calculate the value of 9 raised to power 2 (also known as 9 squared), type the following command line in the terminal and hit ENTER:
let "num1 = 9 ** 2" ; echo $num1
The above image shows the use of the exponent operator and its output.
4. Using Increment/Decrement Operators with “let” Command
You can use the increment or decrement operators to increase or decrease the value of the variables before or after completing a specific task. Here are some examples:
Case 1: Post-increment (id++) and Post-decrement (id–) Operators
To increase or decrease the value of a variable after a specific task, you can use the post-increment or post-decrement operators, respectively. For example:
let "num1 = 5" "num2=num1++"; echo $num1 $num2
let "num1 = 5" "num2=num1--"; echo $num1 $num2
Their output should be like this:
In the image above, you can observe that the variables num1 and num2 are initially assigned to the value 5. Subsequently, the value of the num1 variable is incremented by 1 in the first case, resulting in 6. In the second command line, the num1 variable is decremented by 1, resulting in a value of 4.
Note: When you assign a new value to a previously defined variable in the local shell environment, the previous value gets overwritten by the new value.
Case 2: Pre-increment (++id) and Pre-decrement (–id) Operators
To increase or decrease the value of a variable before a specific task, you can use the pre-increment or pre-decrement operators, respectively. Consider the following command lines:
let "num1 = 5" "num2=++num1"; echo $num1 $num2
let "num1 = 5" "num2=--num1"; echo $num1 $num2
The output will be like this:
From the above image, you can see, in the first command line, num1 starts with a value of 5 and is incremented by 1. Subsequently, num2 is assigned the value of the incremented num1, resulting in both num1 and num2 having a value of 6.
In the second command line, num1 begins with a value of 5 and is then decremented by 1. Afterwards, num2 is assigned the value of the decremented num1, leading to both num1 and num2 ending up with a value of 4.
5. Using “let” Command for Bitwise Operations
To evaluate the bitwise AND, OR, XOR, etc operations, you can use the let command. Follow the examples below for demonstrations:
Case 1: Using Bitwise AND Operator
A bitwise AND (& symbol) operator processes a pair of values by converting them into 32-bit signed integers (depending on the processors) and then comparing the digits sequentially. It checks every bit position of both operands and returns 1 only when both bits are 1; otherwise, it returns 0. Type the following command line in the terminal and hit ENTER to see the output:
let "num1 = 5" "num2 = 6" "num3=num1&num2"; echo $num3
The above image shows the bitwise AND operation between 5 and 6. The output is 4. This is because the binary values for 5 and 6 are 101 and 110, respectively. So their consecutive digit comparison yields 100, which is equal to 4 in decimal.
Case 2: Using Bitwise OR Operator
The bitwise OR (| symbol) operator evaluates the corresponding digits of two values and yields 1 if at least one of the digits is 1; otherwise, it returns 0. Try out the following command line:
let "num1 = 5" "num2 = 6" "num3=num1|num2"; echo $num3
From the image above, you can see the bitwise OR operation between 5 and 6. The binary values for 5 and 6 are 101 and 110. So this time the digit comparison yields 111, which is equal to 7 in decimal.
Case 3: Using Bitwise XOR Operator
The bitwise XOR (^ symbol) operator makes a digit-by-digit comparison of the two values. It returns 0 if the digits match and 1 otherwise. For instance, type the following command in the terminal and hit ENTER:
let "num1 = 5" "num2 = 6" "num3=num1^num2"; echo $num3
The above image shows the bitwise XOR operation between 5 and 6. As you already know, the binary values for 5 and 6 are 101 and 110. Hence, the bit-by-bit XOR comparison results in 011 which is equal to 3 in decimal.
Case 4: Using Bit-wise Negation Operator
The bitwise negation (~ symbol) operator replaces each 1s with 0s and 0s with 1s in a number. For example, consider the following command line:
let "num1 = 5" "num1 = ~num1"; echo $num1
The output image shows the bitwise negation. The 32-bit representation of the number 5 is 000..101 (with 29 zeros in front). The bitwise negation on this number returns 111…010 (with 29 ones in front), which is the two’s complement representation of -6.
Case 5: Using Bitwise Shift Left / Right
To shift the bits of a number to the left or right by a specified number of positions, you can use the bitwise left (<<) or right (>>) operators. For example, try out the following command lines:
let "num1 = 4 << 1"; echo $num1
let "num1 = 4 >> 1"; echo $num1
The image above shows the bitwise shifting operation. The binary representation of 4 is 100. In the first command line, shifting to the left by 1 position results in 1000 which is 8 in decimal. On the other hand, in the second command line, shifting to the right by 1 position results in 10, which is 2 in binary.
Exit Status With “let” Command
The exit status of the let command depends on the evaluation of the rightmost expression. If the last expression evaluates to 0, the exit status is 1 otherwise the exit status is 0. For example, use the following commands to check the exit status of the let command in each case:
let “num1 = 9” “num2 = 5”, echo $?
let “num1 = 9” “num2 = 0”, echo $?
let “num1 = 9” “num2 = -5”, echo $?
See in the image that, the exit status of the let command depends on the evaluation of the rightmost expression. When the rightmost expression (the value of the num2 variable in this example) is nonzero, the exit status is 0. Conversely, when the rightmost expression is 0, the exit status becomes 1. This exit status is useful for debugging, loop control, etc.
Alternatives of “let” Command
Although the let command is quite useful for carrying out arithmetic operations, there exist some alternatives to this command. Let’s see them in this section:
Case 1: Using Arithmetic Expansion or Arithmetic Evaluation
You can use the arithmetic expansion (syntax $((…))) or arithmetic evaluation (syntax ((…))) as an alternative to the let command. For instance, you can use the following command lines for evaluating the arithmetic addition that we’ve done using the let command in example 1:
num1=9; num2=5; num3=$((num1+num2)); echo $num3
num1=9; num2=5; ((num3=num1+num2)); echo $num3
Here, the first command line shows the use of arithmetic expansion, while the second command line is an example of arithmetic evaluation.
In the above image, you can see the use of arithmetic expansion and arithmetic evaluation for performing arithmetic operations.
Case 2: Using “expr” Command as “let” Command Alternative
You can use the expr command as an alternative to the let command. For example, try out the following command lines to evaluate the basic arithmetic operations, like we’ve done in example 1:
num1=9; num2=5; num3=$(expr $num1 + $num2); echo $num3
num1=9; num2=5; num3=$(expr $num1 - $num2); echo $num3
num1=9; num2=5; num3=$(expr $num1 \* $num2); echo $num3
num1=9; num2=5; num3=$(expr $num1 \/ $num2); echo $num3
The above image shows the output of arithmetic operations using the expr command.
Declaring the variable type, like by using declare -i
is optional when you are working with integers.
Conclusion
In this article, I’ve showcased the most common usage of the let command in Linux. You can utilize the let command for evaluating arithmetic operations such as addition, subtraction, multiplication, and bitwise operations like AND, OR, and XOR operations. Experimenting with the examples provided will help you better understand the set command. I hope this article proves helpful in doing so.
People Also Ask
What is the use of let in Linux?
The use of the let command in Linux is to evaluate arithmetic operations. Using the let command in Linux, you can evaluate common arithmetic operations like addition, and subtraction, and bitwise operations like AND, OR, XOR, etc. You can also use this command to assign the result of an arithmetic operation to a variable.
What is the limitation of the let command?
The limitation of the let command in Linux is that it works on expressions that contain only integers. The let command cannot operate on floating point arithmetic.
Can I combine multiple arithmetic operations with a single let command?
Yes, you can combine multiple arithmetic operations with a single let command. To do so, you need to use semicolons as the separator after each arithmetic operation. It is a good practice to enclose individual expressions in double quotation marks when using the let command to prevent potential errors.