LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-07-2005, 07:33 PM   #1
Massif
LQ Newbie
 
Registered: May 2005
Distribution: Trustix
Posts: 10

Rep: Reputation: 0
Decimal numbers in bash script variables?


Is it possible to store fractional numbers in variables in bash scripts? I can only seem to save integers.

If it's not possible, does anyone know how I could do fractional calculations using only integers?
 
Old 11-07-2005, 07:47 PM   #2
tangle
Senior Member
 
Registered: Apr 2002
Location: Smithville, TN
Distribution: Slackware
Posts: 1,745

Rep: Reputation: 71
This is a shot in the dark for me, but did you try to put the numbers in quotes? It works for white spaces, not sure about decimal numbers.
 
Old 11-07-2005, 07:56 PM   #3
Massif
LQ Newbie
 
Registered: May 2005
Distribution: Trustix
Posts: 10

Original Poster
Rep: Reputation: 0
Yeah I tried that.

I actually found my answer. Google

Bash itself cannot support floating point numbers, but there is a program called bc that can do decimal arithmetic.

here's the basic syntax

answer=$(echo "scale=9; $x *1.423" | bc)

The scale value is the number of decimal spaces you want. I'm still not sure if you can initialize a variable to a decimal number, but I got my stuff working.
 
Old 11-07-2005, 10:01 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
You store anything as a "string", but when you want to calculate, you'll have to use bc ...
 
  


Reply


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
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 06:48 PM
How to use decimal numbers in in shell scripts a1ex_007 Programming 6 03-11-2005 10:05 AM
bash script variables twantrd Programming 7 11-17-2004 03:38 AM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 10:57 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 10:16 AM


All times are GMT -5. The time now is 08:49 AM.

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