English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux bc Command

Linux Command大全

The bc command is an arbitrary precision calculator language, usually used as a calculator under Linux.

It is similar to a basic calculator, and with this calculator, you can perform basic mathematical operations.

Common operations:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • ^ Exponent
  • % Remainder

Syntax

bc(options)(arguments)

Option values

  • -i: Force to enter interactive mode;
  • -l: Define the standard mathematical library used
  • ; -w: Provide warning information for POSIX bc extensions;
  • -q: Do not print the normal GNU bc environment information;
  • -v: Display the version information of the command;
  • -h: Display the help information of the command.

Parameters

File: Specify the file containing the calculation task.

Online Example

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+3
5
5-2
3
2+3*1
5

Input quit to exit.

Through the pipe symbol

$ echo "15+5" | bc
20

scale=2 Set decimal places,2 represents retaining two digits:

$ echo 'scale=2; (2.777 - 1.4744)/1' | bc
1.30

bc, in addition to scale for setting decimal places, also has ibase and obase for other base calculations:

$ echo "ibase=2;111" |bc
7

Base Conversion

#!/bin/bash
abc=192 
echo "obase=2;$abc" | bc
<pre>
<p>
Execution result is:11000000, This is converting decimal to binary using bc.</p>
<pre>
#!/bin/bash 
abc=11000000 
echo "obase=10;ibase=2;$abc" | bc

Execution result is:192This is converting binary to decimal using bc.

Calculate the square and square root:

$ echo "10^10" | bc 
10000000000
$ echo "sqrt(100)" | bc
10

Linux Command大全