LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 06-30-2008, 04:51 PM   #1
jeriryan
Member
 
Registered: Apr 2003
Location: United States
Distribution: RHEL 5.4, Snow Leopard
Posts: 87

Rep: Reputation: 15
Adding non-integer numbers stored in variables as strings


I've been trying my hand at a bash script. If I have two variables, bob and betty, and inside bob is the string "1.55", and betty stores the string "2.40", for instance, how can I add them together and store that result in steve?
 
Old 06-30-2008, 05:26 PM   #2
pokemaster
Member
 
Registered: Apr 2005
Location: Massachusetts, USA
Distribution: debian,ubuntu,slackware
Posts: 110

Rep: Reputation: 17
steve=$(( $bob + $betty ))
 
Old 06-30-2008, 09:32 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
For non-integer calculations in Bash you can use bc or dc:-
Code:
steve=$(echo $bob + $betty | bc)
# or
steve=$(dc -e "$bob $betty + p")
 
Old 06-30-2008, 11:50 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by pokemaster View Post
steve=$(( $bob + $betty ))
Au contraire---this works only with integers.
 
Old 07-01-2008, 06:16 AM   #5
pokemaster
Member
 
Registered: Apr 2005
Location: Massachusetts, USA
Distribution: debian,ubuntu,slackware
Posts: 110

Rep: Reputation: 17
I stand corrected


Sorry about 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
how to convert a string of numbers to integer? cigarstub Programming 8 04-09-2007 08:49 PM
Integer Numbers in C Mercurius Programming 24 10-20-2005 10:54 AM
binding an integer to variables... xconspirisist Programming 4 11-05-2003 12:48 PM
length of integer variables daztheladd Programming 5 11-26-2002 08:29 AM


All times are GMT -5. The time now is 07:35 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration