Skip to content

Math

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

Terminal window
echo '2 3 + p' | dc
5
or
dc <<< '2 3 + p'
5

You can print the top element many times

Terminal window
dc <<< '1 1 + p 2 + p'
2
4

For negative numbers use _ prefix

Terminal window
dc <<< '_1 p'
-1

You can also use capital letters from A to F for numbers between 10 and 15 and . as a decimal point

Terminal window
dc <<< 'A.4 p'
10.4

dc 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

Terminal window
dc <<< '4 3 / p'
1

We can increase the precision using command k. 2k will use

Terminal window
dc <<< '2k 4 3 / p'
1.33
dc <<< '4k 4 3 / p'
1.3333

You can also use it over multiple lines

Terminal window
dc << EOF
1 1 +
3 *
p
EOF
6

bc is a preprocessor for dc.

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:

Terminal window
echo '2 + 3' | bc
5
echo '12 / 5' | bc
2

For floating-post arithmetic, you can import standard library bc -l:

Terminal window
echo '12 / 5' | bc -l
2.40000000000000000000

It can be used for comparing expressions:

Terminal window
echo '8 > 5' | bc
1
echo '10 == 11' | bc
0
echo '10 == 10 && 8 > 3' | bc
1

Arithmetic computation can be also done without involving any other programs like this:

Multiplication:

Terminal window
echo $((5 * 2))
10

Division:

Terminal window
echo $((5 / 2))
2

Modulo:

Terminal window
echo $((5 % 2))
1

Exponentiation:

Terminal window
echo $((5 ** 2))
25

expr or Evaluate expressions evaluates an expression and writes the result on standard output

Basic arithmetics

Terminal window
expr 2 + 3
5

When multiplying, you need to escape the * sign

Terminal window
expr 2 \* 3
6

You can also use variables

Terminal window
a=2
expr $a + 3
5

Keep in mind that it only supports integers, so expression like this

Terminal window
expr 3.0 / 2

will throw an error expr: not a decimal number: '3.0'.

It supports regular expression to match patterns

Terminal window
expr 'Hello World' : 'Hell\(.*\)rld'
o Wo

Or 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

Terminal window
expr index hello l
3
expr index 'hello' 'lo'
3