ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
let, expr, and bc - all can be used for Mathematical calculations but the first two commands fail when there is a real number or fractional number to deal with. bc does not help when we need multiplication and division operations.
Let's take this example:
Code:
$ echo "10000 * 100 / 15000" | bc
$ 66
That is not what we expect.
But this one works fine:
Code:
$ echo "10000 - 10.99" | bc
$ 9989.01
But I need to do multiplication and division operations and the resultant values / numbers can be real ones.
How to achieve it in bash?
Well, I do know that Shell Script or Bash can't help us much with Mathematical calculations that require (some or large) precision.
So, wherever in my script I need to do some calculations, what tool or approach should I employ?
A solution is to use bc to do such comparisons. From the bc man page: "Relational expressions are a special kind of expression that always evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is true".
Unfortunately bash's usage of 0 and 1 (not 0) for true and false results in some counter-intuitive bits of code like:
I don't know about you, but $? is 0 whether or not '1 < 2' or '1 > 2' for me.
That checks bc's output, not its exit code.
EDIT: After trying this, I'm not sure:
Code:
$ if [[ 0 ]]; then echo true; fi
true
$ if [[ 1 ]]; then echo true; fi
true
EDIT2: I tried this, too:
Code:
$ if [[ $(false) ]]; then echo true; fi
$ if [[ $(true) ]]; then echo true; fi
$ if [[ $(echo 1) ]]; then echo true; fi
true
$ if [[ $(echo 0) ]]; then echo true; fi
true
$ if [[ $(echo fgdfgsdf) ]]; then echo true; fi
true
It looks like it's checking whether the string is not empty, not the exit code or numeric value.
There are four special variables, scale, ibase, obase, and last. scale defines how some operations use digits after the decimal point. The default value of scale is 0.
...
If bc is invoked with the -l option, a math library is preloaded and the default scale is set to 20. The math functions will calculate their results to the scale set at the time of their call.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.