Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!

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.

I am trying to do a relatively easy arithmetic operation and bash is making it literally impossible. I have been searching google and tutorials for nearly 3 hours and about ready to just give up and say f*ck it.

This is what I'm trying to do:

$x=309
$y=98

I am trying to perform "( y / x ) * 100" to get the percentage as a decimal.

(the answer should be 31.71%)

I have tried a thousand different combinations of things and have come up with nothing. I would like to murder the person who made the arithmetic so hard to perform in bash.

I would be permanently in debt to whoever can help me out.

I'm not sure if bash (or any other shell) can do arithmetic directly,
but it is certainly possible to give the problem to another process,
and return the answer into bash:

that won't work because $x and $y are already whole numbers and i can't simply add a decimal on the end because they are taken from performing a command. They represent the number of files in a certain directory.

This will give you 2 decimal places if you still interested:

Code:

#!/bin/bash
num_to_be_divided=98
num_to_divide_by=309
# to get 2 decimal places multiply num_to_be_divided by 100
new_num_to_be_divided=$(($num_to_be_divided*100))
# do the percentage calculation - not forgetting to multiply before dividing
temp_percent=$(($new_num_to_be_divided*100/$num_to_divide_by))
# get the integer value and decimal places of the real percentage
int_percent=$(($temp_percent/100))
dec_percent=$(($temp_percent%100))
# print the answer
echo $int_percent"."$dec_percent"%"
exit 0

Note that the second decimal place is not completely accurate though.

For example, anything from 31.710 to 31.719 will be 31.71 as there is no rounding up.

If it needs to be more accurate you can get three decimal places and increment the second decimal place as necessary.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.