Simple calculator that performs arithmetic expressions.
let arg [arg ...]
arg: arithmetic expression
Returns 1
when the last expression executed by let
evaluates to 0, otherwise returns 0
.
When the divisor of the expression executed by let
is 0, 1
is returned and an error is reported.
Operator | Description |
---|---|
id++, id-- | increment after variable, decrement after variable |
++id, --id | Variable pre-increment, variable pre-decrement |
-, + | positive sign, negative sign |
!, ~ | Logical No, bitwise negation |
** | Power operation |
*, /, % | ````Multiplication, division, remainder``` |
+, - | Addition, subtraction |
<<, >> | Bitwise shift left or right |
<=, >=, <, > | Compare |
==, != | equal to, not equal to |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
&& | Logical AND |
|| | Logical OR |
expr ? expr : expr | Conditional operator (ternary operator) |
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= | Assignment |
# Try executing arithmetic expressions directly in the terminal (like in python's IDLE).
3+4
bash: 3+4: command not found...
# Another way.
3+4
bash: 3: command not found...
# It seems not possible.
# let command assignment.
let a=3**4
echo ${a}
# Display 81.
# ((...)) is equivalent to the let command.
((a=3**4))
# let is often used for variable assignment, while the external command expr can directly return the value of an expression.
let 3+4
#7 is not displayed.
# After execution, 7 is displayed, pay attention to the spaces.
expr 3 + 4
# Conditional expression.
if ((8>4)); then
echo '8 is greater than 4.'
else
echo 'error'
fi
# Pay attention to the spaces.
if [[ 12 -le 10 ]]; then
echo 'error'
else
echo '12 is greater than 10.'
fi
# You can perform arithmetic operations by setting integer attributes through the declare command.
# The local command is similar to this.
# If the integer attribute is not specified, the output is the string 'a+b'.
declare a=3 b=4 c
c=a+b
echo ${c}
# However, you can assign values in the following ways.
c=$((a+b))
echo ${c}
# show 7
# You can add it directly after setting the integer attribute.
declare -i a=3 b=4 c
c=a+b
echo ${c}
# Same as above.
declare -i a
a=2*3
echo ${a}
# Display 6.