Back to the command reference

bc

ShellFundamentals In simulator In real Linux

Arbitrary-precision calculator

Name: bc = basic calculator

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

bc is an arbitrary-precision calculator language, used interactively or fed expressions through a pipe. The scale variable sets decimal places and -l loads the math library (sine, log …). The shell only does integers, so bc handles decimals.

Syntax

bc [OPTION]... [FILE]...

Common options

-l / --mathlibLoad the math library (20 decimal places)
-q / --quietDo not print the welcome banner
scale=NKeep N decimal places
ibase / obaseInput / output base: echo "obase=2; 10" | bc gives 1010
sqrt( )Square root, e.g. echo "scale=3; sqrt(2)" | bc

Examples

  • $ echo "3.5 * 2" | bc

    Decimal arithmetic

    Explain
  • $ echo "scale=2; 10/3" | bc

    Two decimals: 3.33

    Explain

Related