LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding non-integer numbers stored in variables as strings (https://www.linuxquestions.org/questions/linux-newbie-8/adding-non-integer-numbers-stored-in-variables-as-strings-652647/)

jeriryan 06-30-2008 03:51 PM

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?

pokemaster 06-30-2008 04:26 PM

steve=$(( $bob + $betty ))

Kenhelm 06-30-2008 08:32 PM

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")


pixellany 06-30-2008 10:50 PM

Quote:

Originally Posted by pokemaster (Post 3199638)
steve=$(( $bob + $betty ))

Au contraire---this works only with integers.

pokemaster 07-01-2008 05:16 AM

I stand corrected :)


Sorry about that


All times are GMT -5. The time now is 02:37 AM.