To use expr for calculations in the command line, follow these steps:
Basic Syntax:
expr <expression>
Key Points:
- Spaces: Ensure there are spaces around operators (e.g.,
+,-,*,/). - Operators:
- Addition:
+ - Subtraction:
- - Multiplication:
*(use\*to escape it) - Division:
/ - Modulus:
%
- Addition:
Examples:
-
Addition:
expr 5 + 3Output:
8 -
Subtraction:
expr 10 - 4Output:
6 -
Multiplication:
expr 6 \* 7Output:
42 -
Division:
expr 20 / 4Output:
5 -
Modulus:
expr 10 % 3Output:
1
Using Variables:
You can also use variables with expr:
a=5
b=3
expr $a + $b
Output: 8
Note:
expr is useful for simple calculations, but for more complex arithmetic, consider using bc or arithmetic expansion $((...)).
Feel free to explore more about these commands in LabEx labs!
