LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 03-11-2005, 12:57 AM   #1
a1ex_007
LQ Newbie
 
Registered: Mar 2005
Location: Imphal
Distribution: Fedora Core 3 x86_64
Posts: 26

Rep: Reputation: 15
How to use decimal numbers in in shell scripts


How can I use decimal numbers in a shell script.

eg.
$ echo `expr 5 / 2`
2
doesnt give decimal value. !!!!!!!!!!!!!
 
Old 03-11-2005, 01:52 AM   #2
gerardt
Member
 
Registered: Feb 2005
Location: South Africa
Distribution: SuSE
Posts: 60

Rep: Reputation: 15
Bash does not do floating point numbers. However you can use the utility bc to do floating point within scripts. man bc ;-)
 
Old 03-11-2005, 05:20 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
#!/bin/bash

PI=$(echo "scale=20; 355/113" | bc)
echo "PI = $PI"
 
1 members found this post helpful.
Old 03-11-2005, 08:01 AM   #4
a1ex_007
LQ Newbie
 
Registered: Mar 2005
Location: Imphal
Distribution: Fedora Core 3 x86_64
Posts: 26

Original Poster
Rep: Reputation: 15
Re:How to use decimal numbers in in shell scripts

I have tried using the script with a little modification and it worked .
I want to store the output in a variable
and its not working this way.
........................

#!/bin/bash

PI=echo $( "scale=6; 22/7" |bc)
echo $PI
 
Old 03-11-2005, 08:12 AM   #5
gerardt
Member
 
Registered: Feb 2005
Location: South Africa
Distribution: SuSE
Posts: 60

Rep: Reputation: 15
you need to leave out the echo. Just assign the results of the command substitution to the variable.
 
Old 03-11-2005, 08:57 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: Re:How to use decimal numbers in in shell scripts

Quote:
Originally posted by a1ex_007
I have tried using the script with a little modification and it worked .
I want to store the output in a variable
and its not working this way.
........................

#!/bin/bash

PI=echo $( "scale=6; 22/7" |bc)
echo $PI
The "echo" goes after "$("
Code:
#!/bin/bash

PI=$(echo "scale=6; 22/7" |bc)
echo $PI
 
Old 03-11-2005, 09:05 AM   #7
gerardt
Member
 
Registered: Feb 2005
Location: South Africa
Distribution: SuSE
Posts: 60

Rep: Reputation: 15
Good point I missed that.
 
  


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
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
Shell scripting - Random numbers within a range felixc Linux - Newbie 2 10-09-2005 05:41 PM
Shell scripts??? F_ANTHONY Programming 2 10-27-2004 06:28 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 05:25 PM
URGENT NEED: floatting numbers in BASH shell grueugene Programming 5 03-12-2004 09:25 AM


All times are GMT -5. The time now is 06:48 PM.

Main Menu
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