Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

divide in bash to 1 decimal place

divide in bash to 1 decimal place

divide in bash to 1 decimal place


on a system having no bc, perl, awk, and to which I have no administrator rights (only common user account), with restricted shell (bash), sed and expr available, I need to be able divide numbers to 1 decimal place:

> echo $((10/10))
> echo $((5/10))

I'd like to get 1.0, 0.1, etc (so leading zero for <1)

also builtin let gives 0
> let "m=1/4";echo $m

thanks in advance for a hint!

RE: divide in bash to 1 decimal place

bash doesn't have floating point maths built in, so without 'bc' and it's associated scale=n, decimal places simply are not going to happen.

try expr 4 / 3 and see what the result is.


Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: divide in bash to 1 decimal place


so it looks there is no local solution on this host...

> expr 4 / 3

> expr 1 / 3

RE: divide in bash to 1 decimal place


If you want to keep your script pure Bash, you can do a trick by multiplying the dividend enough so integer arithmetics give precise enough result, then add separate the fractional part with string operation :

CODE --> Bash

m=$(( 4 * 10 / 3 ))
echo "${m:0:-1}.${m: -1}" 

But better use tool instead. Either the already mentioned Bc with its verbose syntax, or Dc :

CODE --> command line

master # bc <<< 'scale=1;4/3'

master # dc <<< '1k4 3/p'


RE: divide in bash to 1 decimal place

If it's for scale 1 why not simply multiply the numerator by 10 and use string manipuation to insert the point.
Maybe something like this (rough draft):





[ $(($X/$Y)) -ge 1 ] && RD=${R:0:${#R}-1}.${R:${#R}-1} || RD=0.$R

echo $RD 
Just keep in mind that your result is a string...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close