Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This 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!


  Search this Thread
Old 03-14-2005, 03:40 PM   #1
LQ Newbie
Registered: Jan 2005
Posts: 22

Rep: Reputation: 0
simple arithmetic in bash

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:


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.

Old 03-14-2005, 03:58 PM   #2
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
The nearest you will get with bash arithmatic is:
echo $((y*100/$x))
Bash only does integers, (as far as I know). So if you do 98/309 you get 0, hence you must multiply 98 by 100 first.

If you want to play around,
will give you the remainder.

With a bit of work you can get decimal places from this and concantenate them to get the result.
Old 03-14-2005, 04:04 PM   #3
Registered: Feb 2005
Location: California, USA
Distribution: Novell SuSE
Posts: 39

Rep: Reputation: 15
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:

z=`python -c "print $y / $x * 100"`
echo $z

Want to use something other than python?
Try Perl or expr.
Old 03-14-2005, 04:15 PM   #4
Registered: Feb 2004
Distribution: Slackware 10.2
Posts: 669

Rep: Reputation: 32
yea expr works as the poster above said

Last edited by Boow; 03-14-2005 at 04:19 PM.
Old 03-14-2005, 04:23 PM   #5
LQ Newbie
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
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.
Old 03-15-2005, 03:47 AM   #6
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Maybe you should look at the bc command. You may need to install it - it did not come automatically installed with Mandrake.
Old 03-15-2005, 12:13 PM   #7
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Well, you could do

or calculate

But those will fail anyway. As ahh pointed out, bash only does integer arithmetic. Quoting

Bash does not understand floating point arithmetic. It treats numbers containing a decimal point as strings.


let "b = $a + 1.3" # Error.
# let: b = 1.5 + 1.3: syntax error in expression (error token is ".5 + 1.3")

echo "b = $b" # b=1

Use bc in scripts that that need floating point calculations or math library functions.
A page I find very good for learning bach is here:

Hope this helps...

Last edited by enemorales; 03-15-2005 at 12:14 PM.
Old 03-15-2005, 06:43 PM   #8
LQ Newbie
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
thanks guys,

but I just ended up multipying the numberator by 100 and said 'screw it' to the decimal, i couldn't figure it out, so I just went with rounded number.

Thanks again,
Old 03-16-2005, 08:50 AM   #9
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
This will give you 2 decimal places if you still interested:


# to get 2 decimal places multiply num_to_be_divided by 100


# do the percentage calculation - not forgetting to multiply before dividing


# get the integer value and decimal places of the real percentage


# 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.
Old 03-16-2005, 02:09 PM   #10
LQ Newbie
Registered: Jan 2005
Posts: 22

Original Poster
Rep: Reputation: 0
Thanks very much,

This should prove to be handy in the future.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple bash problem xushi Programming 2 04-22-2004 07:02 AM
Bash, non-integers and arithmetic causticmtl Programming 5 07-16-2003 09:15 AM
Basic Java arithmetic question chr15t0 Programming 2 07-05-2003 01:32 PM
simple bash q adam_boz Linux From Scratch 1 06-08-2003 11:22 AM
arithmetic operators in Kylix3 (C++) herbie_52 Programming 2 05-23-2003 07:36 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration