Math
Math using dc
dc is one of the oldest language on Unix.
It is using the reverse polish notation, which means that you are first stacking numbers, then operations. For example 1+1 is written as 1 1+.
To print an element from the top of the stack use command p
echo '2 3 + p' | dc
5
or
dc <<< '2 3 + p'
5You can print the top element many times
dc <<< '1 1 + p 2 + p'
2
4For negative numbers use _ prefix
dc <<< '_1 p'
-1You can also use capital letters from A to F for numbers between 10 and 15 and . as a decimal point
dc <<< 'A.4 p'
10.4dc is using abitrary precision which means that the precision is limited only by the available memory. By default the precision is set to 0 decimals
dc <<< '4 3 / p'
1We can increase the precision using command k. 2k will use
dc <<< '2k 4 3 / p'
1.33
dc <<< '4k 4 3 / p'
1.3333You can also use it over multiple lines
dc << EOF
1 1 +
3 *
p
EOF
6
bcis a preprocessor fordc.
Math using bc
bc is an arbitrary precision calculator language. It could be used interactively or be executed from command line.
For example, it can print out the result of an expression:
echo '2 + 3' | bc
5
echo '12 / 5' | bc
2For floating-post arithmetic, you can import standard library bc -l:
echo '12 / 5' | bc -l
2.40000000000000000000It can be used for comparing expressions:
echo '8 > 5' | bc
1
echo '10 == 11' | bc
0
echo '10 == 10 && 8 > 3' | bc
1Math using bash capabilities
Arithmetic computation can be also done without involving any other programs like this:
Multiplication:
echo $((5 * 2))
10Division:
echo $((5 / 2))
2Modulo:
echo $((5 % 2))
1Exponentiation:
echo $((5 ** 2))
25Math using expr
expr or Evaluate expressions evaluates an expression and writes the result on standard output
Basic arithmetics
expr 2 + 3
5When multiplying, you need to escape the * sign
expr 2 \* 3
6You can also use variables
a=2
expr $a + 3
5Keep in mind that it only supports integers, so expression like this
expr 3.0 / 2will throw an error expr: not a decimal number: '3.0'.
It supports regular expression to match patterns
expr 'Hello World' : 'Hell\(.*\)rld'
o WoOr find the index of the first char in the search string
This will throw
expr: syntax erroron Mac OS X, because it uses BSD expr which does not have the index command, while expr on Linux is generally GNU expr
expr index hello l
3
expr index 'hello' 'lo'
3