Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-29-2004, 01:03 PM   #1
Registered: Mar 2004
Location: dublin IRL
Distribution: Slackv12.1, Slamd64v12.1,Xubuntu v8.10_64, FC8_64
Posts: 438
Blog Entries: 5

Rep: Reputation: 32
bash mathematics possible?

Can bash add numbers within a file if the file is simple?
Eg. I have a file with numbers. They're separated by new lines. That's all there is in it.
Can it add them up or perform some maths processing on them?
Old 04-29-2004, 01:43 PM   #2
Senior Member
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 48

sure, why not??
Old 04-29-2004, 02:17 PM   #3
Matt Collier
Registered: Apr 2004
Distribution: Debian
Posts: 80

Rep: Reputation: 15
always baffles me that people use bash for non system stuff when there's far better scripting languages available

#!/usr/bin/perl -w

use warnings;
use strict;

my $file = 'numbers';
my $total;

open NUMBERFILE, $file or die "cannot open file '$file': $!";
while (my $number = <NUMBERFILE>) {
$total += $number;

print "the total of $file is $total\n";
Old 04-29-2004, 03:14 PM   #4
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Though, why use a complicated perl script when a one-line bash command will do?

If your input file is called numbers.txt, then something like this:

let sum=0; for num in `cat numbers.txt`; do let sum=sum+$num; done; echo $sum
There's probably a simpler way to do it, but this'll get the job done.

Last edited by wapcaplet; 04-29-2004 at 05:25 PM.
Old 04-29-2004, 03:39 PM   #5
Senior Member
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
yes, bash can do math on numbers, provided that they're integers and not floating point numbers(ie. 0.0056, 1.5, etc....).
Old 04-30-2004, 04:45 AM   #6
Registered: Mar 2004
Location: dublin IRL
Distribution: Slackv12.1, Slamd64v12.1,Xubuntu v8.10_64, FC8_64
Posts: 438
Blog Entries: 5

Original Poster
Rep: Reputation: 32
Thanks for answers.

I was reading up about "bc" and tried various ways to use it using xargs to pass arguments to it, but it wouldn't give. Needs more research. I guess.

I suppose I want to do this in bash because it's (or its brother and sister shells) available on old and minimal distros. Besides I wouldn't like to assume perl everywhere I go

Actually I am a perl fan, and any chance to brush up is welcome, so thanks for script Matt. Though: isn't "use warnings;" redundant after the -w shebang line?

Naturally I could also load the file into gnumeric and sum off from there but the fun is not only doing it, but finding the most efficent ways of doing it.

BTW all this come from grep -ch *, which counts the occurences of a word in all the files of a directory. I'm going to try out wapcaplet's sugg later on and check out bc a bit more, as it does handle floating point, and looks to be fairly powerful.


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
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 02:57 PM
mathematics ? Volcano Programming 6 10-05-2005 11:17 PM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM
Bash marco271 Programming 6 07-19-2003 04:30 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:15 PM.

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