Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
2
answers
92
views
Can bc explain itself instead of just giving you an answer?
When you do math in `bc` it clearly uses some order of operations. And I know that this falls within the realm of Reverse Polish Notation; so is it possible for it to explain itself? I mean I know I can do order of operations on my own, but recently there's been some disagreement online about this,...
When you do math in
bc
it clearly uses some order of operations.
And I know that this falls within the realm of Reverse Polish Notation; so is it possible for it to explain itself?
I mean I know I can do order of operations on my own, but recently there's been some disagreement online about this, and it would be good to be able to determine if someone used PEMDAS or something else to calculate their result.
If each of the steps were shown within the program that would clear all of that up.
leeand00
(4937 rep)
May 30, 2025, 04:51 PM
• Last activity: Jun 5, 2025, 05:12 PM
4
votes
1
answers
244
views
using the bc calculator with obase and ibase to convert integer representation
I am using the bc calculator with `obase` and `ibase` to convert integer representation. I'm not used to it, because very basic things can be done with `$(( expresion ))` in bash, and most things that can't be done with that are prone to get more complicated in the future, and then, perl or java are...
I am using the bc calculator with
obase
and ibase
to convert integer representation.
I'm not used to it, because very basic things can be done with $(( expresion ))
in bash, and most things that can't be done with that are prone to get more complicated in the future, and then, perl or java are the tools of choice.
What I have done is
helper@gg$ echo 'obase=10; ibase=16;F;' | bc
15
helper@gg$ echo 'ibase=16; obase=10;F;' | bc
F
helper@gg$ echo 'ibase=16; obase=10;10;' | bc
10
helper@gg$ echo 'obase=10; ibase=16;10;' | bc
16
helper@gg$
and the result obviously depends on the order in which ibase and obase are given.
Naturally, I've taken a look at the bc manpage but found noting about this.
Question is: how does it work, and where can I read more about this, to avoid other pitfalls in the like of this.
Gyro Gearloose
(455 rep)
May 27, 2025, 01:03 PM
• Last activity: May 27, 2025, 01:52 PM
15
votes
6
answers
8599
views
how to make bc to show me 10 and not 10.00
#!/bin/bash q=$(bc secondcoordinate.txt That's just part of the script, but I think it's enough to clarify my intentions. `p` is a variable with just two decimals, so `q` should be an integer... Nevertheless, `bc` shows, for example, `10.00` instead of `10`. How can I solve this?
#!/bin/bash
q=$(bc secondcoordinate.txt
That's just part of the script, but I think it's enough to clarify my intentions.
p
is a variable with just two decimals, so q
should be an integer... Nevertheless, bc
shows, for example, 10.00
instead of 10
.
How can I solve this?
Diego
(319 rep)
Nov 3, 2016, 12:13 AM
• Last activity: Mar 21, 2025, 11:02 AM
57
votes
5
answers
34654
views
How is bc different from dc?
What are the differences between `dc` and `bc` calculators? When should I use `dc` and when `bc`?
What are the differences between
dc
and bc
calculators?
When should I use dc
and when bc
?
syntagma
(12801 rep)
Apr 13, 2014, 09:30 AM
• Last activity: Mar 11, 2025, 03:21 PM
7
votes
7
answers
10178
views
How do I get bc to start decimal fractions with a leading zero
How do I get `bc` to start decimal fractions with a leading zero? $ bc <<< 'scale=4; 1/3' .3333 I want 0.3333.
How do I get
bc
to start decimal fractions with a leading zero?
$ bc <<< 'scale=4; 1/3'
.3333
I want 0.3333.
Petr Skocik
(29590 rep)
Jun 25, 2016, 05:02 PM
• Last activity: Feb 27, 2025, 05:52 PM
11
votes
6
answers
34664
views
Bash limiting precision of floating point variables
In Ubuntu `14.04.1 LTS 64-bit` bash I am declearing floating point variables by multiplying floating point bash variables in `bc` with scale set to 3; however, I cannot get the number of digits after the decimal point to be zero and get rid of the zero to the left of the decimal point. How can I tra...
In Ubuntu
14.04.1 LTS 64-bit
bash I am declearing floating point variables by multiplying floating point bash variables in bc
with scale set to 3; however, I cannot get the number of digits after the decimal point to be zero and get rid of the zero to the left of the decimal point. How can I transform, say 0.005000000
into .005
? This is necessary due to my file naming convention. Thanks for your recommendations.
UPDATE: Can I use it for already defined shell variables and redefining them? The following code gives me an error.
~/Desktop/MEEP$ printf "%.3f\n" $w
bash: printf: 0.005000: invalid number
0,000
The output of locale
@vesnog:~$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=tr_TR.UTF-8
LC_TIME=tr_TR.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=tr_TR.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=tr_TR.UTF-8
LC_NAME=tr_TR.UTF-8
LC_ADDRESS=tr_TR.UTF-8
LC_TELEPHONE=tr_TR.UTF-8
LC_MEASUREMENT=tr_TR.UTF-8
LC_IDENTIFICATION=tr_TR.UTF-8
LC_ALL=
The output of echo $w
@vesnog:~$ echo $w
0.005000
Vesnog
(689 rep)
Dec 24, 2014, 09:09 AM
• Last activity: Feb 26, 2025, 02:07 AM
7
votes
2
answers
1926
views
Why does Debian not include POSIX-specified commands like bc and ed by default?
[POSIX.1-2001](https://pubs.opengroup.org/onlinepubs/009695399/) [Utilities](https://pubs.opengroup.org/onlinepubs/009695399/idx/utilities.html) and [POSIX.1-2008](https://pubs.opengroup.org/onlinepubs/9699919799/) [Utilities](https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html) both...
[POSIX.1-2001](https://pubs.opengroup.org/onlinepubs/009695399/) [Utilities](https://pubs.opengroup.org/onlinepubs/009695399/idx/utilities.html) and [POSIX.1-2008](https://pubs.opengroup.org/onlinepubs/9699919799/) [Utilities](https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html) both list the commands
bc
and ed
to be part of POSIX.
Yet, in a brand new Debian installation (version 10 for example), these commands are missing by default:
$ bc
bash: bc: command not found
$ ed
bash: ed: command not found
Why does Debian not include these commands by default? Of course, I can install them with and I did that.
apt-get install bc ed
The bc
binary is only 87K in size. The entire package including the man page and documentation is only 209K in size.
Similarly the ed
binary is only 55K in size. The entire package is only 93K in size.
What good reason is there not to include these tiny packages even though they are specified in POSIX?
Lone Learner
(170 rep)
Sep 12, 2020, 07:00 AM
• Last activity: Feb 23, 2025, 08:08 AM
0
votes
2
answers
1791
views
GNU BC: How is the “modulo” (%) with scale other than 0 useful?
This is a **self-answered** question, the research that is reasonable to ask for a question goes in the answer part, Please do not downvote because you believe I have not researched enough for an answer. Thanks. In any case, there is no description (that I can find) of this characteristic of bc in t...
This is a **self-answered** question, the research that is reasonable to ask for a question goes in the answer part, Please do not downvote because you believe I have not researched enough for an answer. Thanks. In any case, there is no description (that I can find) of this characteristic of bc in this site.
When using
bc
, the %
operator is claimed to calculate the "remainder", and yes, it works for integer numbers and when the **scale is zero**:
$ bc <<<' scale=0; 27 % 7 '
6
But it fails to give the "integer remainder" if the scale is not zero:
$ bc <<<' scale=10; 27 % 7 '
.0000000003
Why (or how) is this definition of the %
modulo useful?
user232326
Oct 28, 2018, 02:02 AM
• Last activity: Nov 25, 2024, 10:48 PM
72
votes
1
answers
8025
views
Why is 'bc' required to build the Linux kernel?
[The Linux kernel minimal building requirements][1] specifies that the calculator `bc` is required to build kernel v4.10, the minimal version of the tool being `1.06.95`. What use is made of `bc` in this context, and why isn't the C language directly used instead of `bc` for these operations? [1]: h...
The Linux kernel minimal building requirements specifies that the calculator
bc
is required to build kernel v4.10, the minimal version of the tool being 1.06.95
.
What use is made of bc
in this context, and why isn't the C language directly used instead of bc
for these operations?
Ikaros
(912 rep)
Apr 23, 2018, 01:45 PM
• Last activity: Oct 4, 2024, 12:32 PM
-2
votes
2
answers
201
views
How to make bc handle explicitly positive numbers? Or what alternative calculator can be used with such support?
Consider the following: ```shell $ echo "-1" | bc -1 $ echo "(1)" | bc 1 $ echo "+1" | bc (standard_in) 1: syntax error ``` Minus sign and round braces are handled correctly but when explicit plus sign is specified `bc` falls with error. Is it possible to make it treat explicitly positive numbers ju...
Consider the following:
It is strange that
$ echo "-1" | bc
-1
$ echo "(1)" | bc
1
$ echo "+1" | bc
(standard_in) 1: syntax error
Minus sign and round braces are handled correctly but when explicit plus sign is specified bc
falls with error. Is it possible to make it treat explicitly positive numbers just as normal positive numbers?
I want to be able to calculate constructions like:
- +1++2
(result must be 3)
Even simple Ubuntu calculator can do such operations:

bc
can't. Seems like a bug.
As a workaround sed
can be used to preprocess the input for bc
, or perhaps bc
has some specific config for this? Or may be there is some alternative to bc
for that?
---
**Update**
I've sent request to bc
's support team to consider to fix this bug.
Comments from [Kusalananda](https://unix.stackexchange.com/users/116858/kusalananda) :
> bc
is a POSIX utility, and any implementation of the utility
> therefore should follow the [POSIX specification](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/bc.html) . This specification
> contains a grammar which does not include the unary plus operation. If
> GNU implements a unary plus in their implementation of bc
, they
> would need to ensure that all existing code that uses bc
still
> performs as before and that any future code that uses bc
performs as
> a reader of the standard would expect.
>
> Also note that -1--2
is an error, so you seem to additionally
> propose a change in the precedence of the increment and decrement
> operators, right?
My comment:
> I think that bc
should be able to perform simple calculations like
> simple calculator can do. If support team adds to bc
at least
> ability to handle cases with digits like -1--2 (with result 1) and
> +1++2 (with result 3) (only unary plus operation for digits, and not falling with error while trying to execute increment / decrement
> operations on digits which all the same are not supported by now) in my understanding it will
> not anyhow impact existing or future POSIX-compliant code.
Anton Samokat
(289 rep)
May 11, 2024, 11:57 AM
• Last activity: Jun 17, 2024, 09:48 AM
-2
votes
1
answers
281
views
Why does bc not have easy to use built in support for rounding?
I just wrote a small bash shell script and for the first time in that context I wanted to make a calculation, a division, and discovered `bc`. My script processes media files and I want to log the performance, that is, the length of the file divided by the duration of the processing. Typical values...
I just wrote a small bash shell script and for the first time in that context I wanted to make a calculation, a division, and discovered
bc
.
My script processes media files and I want to log the performance, that is, the length of the file divided by the duration of the processing. Typical values for the quotient is 0,03-0,05 but bc doesn't round, it truncates! Which is a problem because the difference between 0,0300 and 0,0399 is quite significant but bc will return 0,03 for both.
I have read multiple questions about making bc round, with mostly quite convoluted solutions. This made me wonder:
**Why doesn't bc have native support for rounding??**
I am not looking for a solution but am curious about the design decisions behind the lack of support for rounding. I am aware of the KISS philosophy but rounding floating point values is such a basic feature for any calculator.
Of course you could display the result with two more digits and the make the rounding manually (in your head), but it is weird to waste human mental energy a task that a computer is much better on performing than a human! Hence my question.
d-b
(2047 rep)
May 24, 2024, 07:25 PM
• Last activity: May 27, 2024, 06:37 PM
1
votes
1
answers
55
views
Why does "bc" read my config file on one computer and not another?
I have two old machines, running CentOS 6.5 and CentOS 6.10. Both have bc 1.06.95-1.el6 installed. One reads $HOME/.bc.rc, one does not. Why ? (How??) strace shows near-identical output, except one reads the config file. The libraries are identical.
I have two old machines, running CentOS 6.5 and CentOS 6.10.
Both have bc 1.06.95-1.el6 installed.
One reads $HOME/.bc.rc, one does not. Why ? (How??)
strace shows near-identical output, except one reads the config file. The libraries are identical.
Andrew Daviel
(63 rep)
Apr 21, 2024, 01:50 AM
• Last activity: Apr 21, 2024, 02:42 PM
2
votes
1
answers
96
views
Use m4 interactively with bc
I would like to use `m4` interactively as a preprocessor for `bc`. My immediate use case is to add an `include` capability to `bc`, but I can foresee other uses in the future. But there is a problem. Here is an example using `bc` on its own: ```` $ bc bc 1.07.1 Copyright 1991-1994, 1997, 1998, 2000,...
I would like to use
m4
interactively as a preprocessor for bc
. My immediate use case is to add an include
capability to bc
, but I can foresee other uses in the future. But there is a problem. Here is an example using bc
on its own:
`
$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3+7
10
sqrt(12345)
111
quit
$
`
And here is the same example using m4
interactively:
`
$ m4 -iP - | bc
3+7
10
sqrt(12345)
111
quit
$
`
There are two differences. First, the m4
version suppresses the banner. Second, the m4
version doesn't execute quit
immediately, but instead requires the enter key to be pressed an additional time before it executes the quit
.
Why do these things occur? How can I make the m4
version work the same way as the bc
version?
user448810
(131 rep)
Apr 9, 2024, 08:03 PM
• Last activity: Apr 12, 2024, 10:16 PM
0
votes
3
answers
134
views
bash array multiplication using bc
I am trying to multiply array values with values derived from the multiplication of a loop index using bc. #!/bin/bash n=10.0 bw=(1e-3 2.5e-4 1.11e-4 6.25e-5 4.0e-5 2.78e-5 2.04e-5 1.56e-5 1.29e-5 1.23e-5 1.0e-5) for k in {1..11};do a=$(echo "$n * $k" | bc) echo "A is $a" arn=${bw[k-1]} echo "Arn is...
I am trying to multiply array values with values derived from the multiplication of a loop index using bc.
#!/bin/bash
n=10.0
bw=(1e-3 2.5e-4 1.11e-4 6.25e-5 4.0e-5 2.78e-5 2.04e-5 1.56e-5 1.29e-5 1.23e-5 1.0e-5)
for k in {1..11};do
a=$(echo "$n * $k" | bc)
echo "A is $a"
arn=${bw[k-1]}
echo "Arn is $arn"
b=$(echo "$arn * $a" | bc -l)
echo "b is $b"
#echo $a $b
done
I am able to echo the array values by assigning it to a new variable within the loop, but when I use that to multiply using
bc
, I get (standard_in) 1: syntax error
.
I searched for clues and tried some but none helped. The expected output is as follows.
10 1.00E-02
20 5.00E-03
30 3.33E-03
40 2.50E-03
50 2.00E-03
60 1.67E-03
70 1.43E-03
80 1.25E-03
90 1.16E-03
100 1.23E-03
110 1.10E-03
All help is greatly appreciated.
csnl
(35 rep)
Mar 23, 2024, 07:00 PM
• Last activity: Mar 23, 2024, 10:46 PM
3
votes
1
answers
351
views
By default, bc interprets any alpha character as a 9
**System**: - Ubuntu 22.04.3 LTS - GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) - bc 1.07.1 **Observation**: Both `ibase` and `obase` are unset. echo "A0" | bc 90 echo "B0" | bc 90 echo "X0" | bc 90 **Question**: Why does `bc` interpret alpha characters as `9`s by default? Why wouldn't...
**System**:
- Ubuntu 22.04.3 LTS
- GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
- bc 1.07.1
**Observation**:
Both
ibase
and obase
are unset.
echo "A0" | bc
90
echo "B0" | bc
90
echo "X0" | bc
90
**Question**:
Why does bc
interpret alpha characters as 9
s by default? Why wouldn't an error message be preferable here?
yossi-matkal
(193 rep)
Mar 23, 2024, 04:04 PM
• Last activity: Mar 23, 2024, 06:33 PM
0
votes
0
answers
44
views
is it possible to integrate bc calculator in an app (script interpreter) as a library?
is it possible to integrate bc command line calculator in an app (script interpreter) as a library? I am not asking if that can be done, I am asking if there is something ready already that wraps bc and make it work as a library. In ubuntu 22.04 I can't find any packages like bc-dev libbc-dev libbc...
is it possible to integrate bc command line calculator in an app (script interpreter) as a library?
I am not asking if that can be done, I am asking if there is something ready already that wraps bc and make it work as a library.
In ubuntu 22.04 I can't find any packages like bc-dev libbc-dev libbc ...
Searching for this subject on google doesnt help either.
My goal is to integrate the command line calculator interpreter
bc
inside a GPL game script interpreter called Arx Libertatis (Arx Fatalis).
So, when the game starts and it reads and interprets it's .asl script, it will find a line like ex.:
Calc @result "(~@val1~*33)/~@val2~"
(where ~@val1~ will expand to 0.25 and ~@val2~ will expand to 7.123)
That script line shall send "(0.25*33)/7.123" to bc and assign it's result at @result.
But for that, I need to access the bc main function directly and not call the compiled bc binary.
So I am loogin for a libbc-dev somewhere, but I just can't find it.
Will I have to wrap it in a lib myself?
btw, it must have high performance, and if there is an alternative to bc
that provides a faster result, it is important because it is a game and the calculations must happen as fast as possible.
Also, I could use flags to the Calc script command to determine what calculator interpreter will be used so Calc -b ...
would use bc
, while Calc -d ...
could use some other library (and therefore mod developers must prepare it in a syntax that is recognized by the other library).
Gussak Thor
(11 rep)
Dec 7, 2023, 04:11 PM
• Last activity: Dec 7, 2023, 04:34 PM
47
votes
6
answers
71043
views
Append to a pipe and pass on?
I have a simple bash function dividing two numbers: echo "750/12.5" | bc I'd like to take the output from `bc` and append `/24` and pipe said result to another instance of `bc`. Something like: echo "750/12.5" | bc | echo $1 + "/24" | bc Where `$1` is the piped result. *P.S. I realize I could just d...
I have a simple bash function dividing two numbers:
echo "750/12.5" | bc
I'd like to take the output from
bc
and append /24
and pipe said result to another instance of bc
.
Something like:
echo "750/12.5" | bc | echo $1 + "/24" | bc
Where $1
is the piped result.
*P.S. I realize I could just do echo "750/12.5/24" | bc
my question is more in regards to the appending of text to a pipe result.*
Philip Kirkbride
(10746 rep)
Apr 22, 2017, 12:00 AM
• Last activity: Sep 21, 2023, 09:22 AM
71
votes
4
answers
74664
views
Are operations on floats supported with bc?
It looks like bc doesn't support float operations. When I do `echo 1/8 | bc` it gets me a zero. I checked the manual page `bc (1)`, but it doesn't even mention `float`, so I wonder if it's supported?
It looks like bc doesn't support float operations. When I do
echo 1/8 | bc
it gets me a zero. I checked the manual page bc (1)
, but it doesn't even mention float
, so I wonder if it's supported?
daisy
(55777 rep)
Mar 4, 2013, 02:07 AM
• Last activity: Aug 29, 2023, 11:36 AM
2
votes
2
answers
896
views
How do you derive a decimal from a math operation involving a variable?
I'm trying to find derive `SCALEFACTOR` which is basically `10000/(sum of 4th column in a file)`. How do I get a decimal from the output? Appreciate any help in advance. #!/bin/bash FILES=/path/to/files/*; for f in ${FILES} do echo $f COLTOTAL="$(awk '{sum += $4} END {print sum}' $f)" echo "total: $...
I'm trying to find derive
SCALEFACTOR
which is basically 10000/(sum of 4th column in a file)
. How do I get a decimal from the output? Appreciate any help in advance.
#!/bin/bash
FILES=/path/to/files/*;
for f in ${FILES}
do
echo $f
COLTOTAL="$(awk '{sum += $4} END {print sum}' $f)"
echo "total: ${COLTOTAL}"
# SCALEFACTOR=expr 10^5 / $COLTOTAL
B=10000
SCALEFACTOR=$((B / ${COLTOTAL}))
SCALINGFACTOR=$(echo "100000 / $COLTOTAL" | bc -l
# echo "scale=5; ${SCALEFACTOR}" | bc
echo ${SCALEFACTOR}
awk '{print($1"\t"$2"\t"$3"\t"$4 * ${SCALINGFACTOR})}' $f > $f"_normalized.txt"
done
Stephen
(141 rep)
Feb 24, 2015, 06:48 PM
• Last activity: Feb 17, 2023, 12:41 AM
3
votes
4
answers
4154
views
I keep receiving command not found when I try to run my code in bash
I am trying to write a code that will calculate the radius and area of a circle using pi and the circumference. When I try to run my code, I receive an error message that says ```none main.sh: line 5: bc: command not found main.sh: line 7: bc: command not found ``` Could you please advise me on wher...
I am trying to write a code that will calculate the radius and area of a circle using pi and the circumference. When I try to run my code, I receive an error message that says
main.sh: line 5: bc: command not found
main.sh: line 7: bc: command not found
Could you please advise me on where I went wrong, and help me to fix my code?
The code:
#!/bin/bash
pi=3.14;
circumference=5;
echo "$circumference"
radius=$(echo "$circumference" \* 2 \* "$pi" |bc -l);
echo "$radius"
area=$(echo "$pi" \* "$radius" \* "$radius" |bc -l);
echo "$area"
echo "The radius of a circle is $radius";
echo "The area of a circle is $area";
exit
user445702
(51 rep)
Dec 9, 2020, 06:21 PM
• Last activity: Jan 29, 2023, 09:34 PM
Showing page 1 of 20 total questions