Questions tagged [bc]

an arbitrary precision calculator language used in shell scripting. Use this tag for questions for programming-related uses of bc; general questions about bc usage and troubleshooting are a better fit at Unix & Linux Stack Exchange.

bc, for basic calculator, is "an arbitrary-precision calculator language" using algebraic infix notation. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell.

Typical Usages

There are two ways bc is typically used.

  • From a Unix command prompt, then interactively entering a mathematical expressions, such as (1 + 3) * 2, which will display 8.

  • From the shell command prompt, where bc reads from standard input. For example $ echo '(1 + 3) * 2' | bc will display 8 in the shell.

While bc can perform calculations to arbitrary precision, its default behavior is to truncate calculations to whole numbers. I.e. entering the expression 2/3 displays 0. This can surprise new bc users. The number of digits that bc displays is determined by a variable scale, which may be changed. For example, running bc interactively, setting scale=7 then entering 2/3 will display .6666666. Starting bc with the -l option loads a math library, setting scale to 20 and loading these math functions

s (x)    The sine of x, x is in radians.
c (x)    The cosine of x, x is in radians.
a (x)    The arctangent of x, arctangent returns radians.
l (x)    The natural logarithm of x.
e (x)    The exponential function of raising e to the value x.
j (n,x)  The bessel function of integer order n of x.

In 1991, POSIX rigorously defined and standardized bc. Two implementations of that standard survive today:

  1. Traditional bc, on Unix and Plan 9 systems.

  2. GNU bc with numerous extensions beyond the POSIX standard, on Linux, etc.

Links & References

281 questions
0
votes
1 answer

irritating: bc float expression leads to integer result

I have been happily using bc for a few weeks. Now I need to do some simple calculations like 1.0+27.0/37.0*5.0, which bc in interactive mode calculates as integer result. I tried this on two different linux boxes. Same result: 1 instead of…
0
votes
1 answer

bash -bc curl comparing variables

pretty simple script but I am having issues with it. It will not compare the 2 variables, is this due to floating points or? I tried to use the | bc but still not working... #!/bin/bash x=$(curl -o /dev/null -s -w %{time_total}\\n …
OblongMedulla
  • 1,471
  • 9
  • 21
0
votes
1 answer

Bash, arithmetic expression + bc

I have small problem with use bc command in unix. I have two varaibles: variable1, variable2. The arithmetic expression looks like: res=$$((($variable1*10)/$variable2) I would like to round the result from two divided numbers. I think, the best…
profiler
  • 567
  • 2
  • 15
  • 41
0
votes
2 answers

How do you Pipe hex data directly to bc and converting it to binary - cleanly?

Hex (character) to binary conversion is useful, especially when you want to look at a few bits mushed inside some hex string. It is for this reason that I would like to pipe some hex data to bc (the Unix program known as 'basic calculator') and…
Xofo
  • 1,256
  • 4
  • 18
  • 33
0
votes
2 answers

bc does not completely convert long hexadecimal numbers

I am using bc to convert a long hex vectors to binary. It does not work for the following code example in my awk script: #!/bin/awk -f cmd = "bc <<< \"ibase=16;obase=2;"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\"" result = ((cmd |…
valar_m
  • 35
  • 1
  • 5
0
votes
2 answers

bc: prevent "divide by zero" runtime error on multiple operations

I'm using bc to do a series of calculation. I'm calling it though a bash script that first of all puts all the expressions to be calculated in a single variable, the passes them to bc to calculate the results. The script is something like…
giocarmine
  • 580
  • 1
  • 13
  • 29
0
votes
1 answer

How to overcome *-expansion in this command?

I am trying to write a very simple script using bc. My script is saved as cl and contains #!/bin/bash echo "$@" | bc But running cl 2 * 3 gives several (standard_in) 1: syntax error lines. Running cl 2*3 gives correct output. I believe * is…
MAKZ
  • 165
  • 14
0
votes
1 answer

Calculating days with BC bash

So my script has to calculate day when someone will be back from trip. For calculating using user input for leaving day and length of trip. Everyday has certain number like Monday-1; Tuesday-2 etc but it gets complex when user input for Sunday must…
Gen
  • 178
  • 9
0
votes
1 answer

Code 128 (dhl) barcode and something missing in bars

Currently I'm creating dhl label using zpl command. I'm new to zpl commands. I have used BC - zpl command for code 128 barcode. This is my code: ^BCN,,N,N^FD>:2LDE44536+99000900153004^FS My problem: Above zpl code create barcodes, but when I check…
0
votes
3 answers

Exitting bc in UNIX script

I have to write a script with the bc command, and in doing so I am supposed to exit the bc without any input from the user. So basically the program will still run after the bc is used without waiting for input from the user like my program is…
Randy Gilman
  • 457
  • 1
  • 11
  • 21
0
votes
2 answers

cannot get complex calculation to work in bc

I use qalculate as my day-to-day calculator and it's great! It is easy enough to type in something like: (1+10^(-17.2/20)) / (1-10^(-17.2/20)) and get the right answer: 1.320289 But trying to get bc to do this sort of calculation in a bash script…
scriptz
  • 515
  • 2
  • 10
0
votes
1 answer

Getting (standard_in) 1: parse error with bc command

I am getting Getting (standard_in) 1: parse error with below code. #!/bin/sh base=01C71C mid=0 end=0 for mid in {0..15..1} do for end in {0..15..4} do if (( mid > 9 )) then mid_hex=`echo "obase=16;ibase=10; $mid" | bc` else …
shah_pankil
  • 41
  • 1
  • 1
  • 5
0
votes
2 answers

Parsing Numbers from a File and using those to calculate

I seem to be to stupid to parse some HTML Files with Bash. We have some files which have lines like: var A4_total = 2018 + 4730; var Other1_total = 3242 + 3828; (They tell us how many pages the Printers have printed). I need to calculate the first…
schannall
  • 5
  • 1
0
votes
1 answer

Why do I not see the full expected range of random numbers?

I would expect the below code to generate (quasi) random numbers between 0.9 and 1.0 for RH. randno5=$((RANDOM % 100001)) upper_limit5=$(echo "scale=10; 1*1.0"|bc) lower_limit5=$(echo "scale=10; 1*0.9"|bc) range5=$(echo "scale=10;…
0
votes
0 answers

bc2cnf is not recognised as an internal or external command

bcfile and would like to convert it to a.cnf` file, and hence i wanted to use bc2cnf. However, it keeps giving me the error 'bc2cnf' is not recognized as an internal or external command, operable program or batch file. There was only one package…
Hanzawa Naoki
  • 505
  • 1
  • 8
  • 19