LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 03: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, 04: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, 08:32 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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, 10:50 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by pokemaster View Post
steve=$(( $bob + $betty ))
Au contraire---this works only with integers.
 
Old 07-01-2008, 05: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



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 07:49 PM
Integer Numbers in C Mercurius Programming 24 10-20-2005 09:54 AM
binding an integer to variables... xconspirisist Programming 4 11-05-2003 11:48 AM
length of integer variables daztheladd Programming 5 11-26-2002 07:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:16 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
Open Source Consulting | Domain Registration