Math
Math using dc
Section titled “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' | dc5
or
dc <<< '2 3 + p'5You can print the top element many times
dc <<< '1 1 + p 2 + p'24For 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 << EOF1 1 +3 *pEOF6bc is a preprocessor for dc.
Math using bc
Section titled “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' | bc5
echo '12 / 5' | bc2For floating-post arithmetic, you can import standard library bc -l:
echo '12 / 5' | bc -l2.40000000000000000000It can be used for comparing expressions:
echo '8 > 5' | bc1
echo '10 == 11' | bc0
echo '10 == 10 && 8 > 3' | bc1Math using bash capabilities
Section titled “Math 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
Section titled “Math using expr”expr or Evaluate expressions evaluates an expression and writes the result on standard output
Basic arithmetics
expr 2 + 35When multiplying, you need to escape the * sign
expr 2 \* 36You can also use variables
a=2expr $a + 35Keep 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 error on 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 l3
expr index 'hello' 'lo'3