A simple calculator for performing arithmetic expressions.
let arg [arg ...]
arg: Arithmetic expression
Returns 1 if the result of the last expression evaluated by let is 0; otherwise returns 0.
Returns 1 and an error message if an expression executed by let involves division by zero.
| Operator | Description |
|---|---|
id++, id-- |
Variable post-increment, post-decrement |
++id, --id |
Variable pre-increment, pre-decrement |
-, + |
Unary minus, unary plus |
!, ~ |
Logical negation, bitwise negation |
** |
Exponentiation |
*, /, % |
Multiplication, division, modulo |
+, - |
Addition, subtraction |
<<, >> |
Bitwise left shift, right shift |
<=, >=, <, > |
Comparison |
==, != |
Equality, inequality |
& |
Bitwise AND |
^ |
Bitwise XOR |
| ` | ` |
&& |
Logical AND |
| ` | |
expr ? expr : expr |
Conditional (ternary) operator |
=, *=, /=, %=, +=, -=,`<<=, >>=, &=, ^=, |
=` |
# Attempting to execute an arithmetic expression directly in the terminal (like Python IDLE).
3+4
bash: 3+4: command not found...
# Trying another way.
3 + 4
bash: 3: command not found...
# It seems it doesn't work directly.
# Using the let command for assignment.
let a=3**4
echo ${a}
# Output: 81
# ((...)) is equivalent to the let command.
((a=3**4))
# let is commonly used for variable assignment, while the external command expr can directly return the value of an expression.
let 3+4
# No output (7 is not displayed).
# This outputs 7; note the spaces.
expr 3 + 4
# Conditional expressions.
if ((8>4)); then
echo '8 is greater than 4.'
else
echo 'error'
fi
# Note the spaces in the [[]] form.
if [[ 12 -le 10 ]]; then
echo 'error'
else
echo '12 is greater than 10.'
fi
# Arithmetic operations can be performed by setting the integer attribute with the declare command.
# The local command works similarly.
# Integer attribute not specified; the output is the string 'a+b'.
declare a=3 b=4 c
c=a+b
echo ${c}
# However, you can assign it like this:
c=$((a+b))
echo ${c}
# Output: 7
# With the integer attribute set, you can perform addition directly.
declare -i a=3 b=4 c
c=a+b
echo ${c}
# Same as above.
declare -i a
a=2*3
echo ${a}
# Output: 6
help command for more information.let, other commands for arithmetic calculation include external commands like expr and bc.