Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding numbers in shell script (easy quest)

0 views
Skip to first unread message

Brien M. Oberstein

unread,
Jul 9, 1994, 1:33:24 PM7/9/94
to
Here's an easy question, how do you add numbers in a shell script?
I want to hav a script which does something like echo '$2+20'
ie add 20 to the value of the argument $2


-brien

Mark Thomas

unread,
Jul 9, 1994, 2:38:02 PM7/9/94
to

br...@cco.caltech.edu (Brien M. Oberstein) writes:

: Here's an easy question, how do you add numbers in a shell script?


: I want to hav a script which does something like echo '$2+20'
: ie add 20 to the value of the argument $2

In the c-shell ( csh ) :

(btw, variable names must begin with a letter...)

set $foo = 2;
@ foo = $foo + 20;
echo $foo;

(produces: 22)

In the bourne shell ( sh ) :

foo=2;
foo=`expr $foo + 20`;
echo $foo;

(produces: 22)

Hope it helps,

-mark
--
Mark Thomas
Assistant System Administrator | Student System Administrator
O'Reilly & Associates, Inc. | Boston University CS Department
90 Sherman Street | 111 Cummington Street
Cambridge, MA 02140 | Boston, MA 02215
ma...@ora.com | mth...@cs.bu.edu

:) :) :)

Johan Svensson

unread,
Jul 9, 1994, 3:00:31 PM7/9/94
to

Try this:

echo `expr $2 + 20`


PLEASE NOTE: The "'" should be typed the other way around
("accent grave" in French)!
Unfortunately, my News editor wouldn't let me
do that trick...


Regards,
__________________________________________________________
Johan Svensson Email: jo...@ECSDG.lu.se
Tel: +46-46-104505
EkonomiCentrum Software Development Group, Lund University
URL: <A HREF="http://ecsdg.lu.se/">ECSDG Home Page</A>
----------------------------------------------------------
Address: JoS-Ware Comp Tech, Box 739, 220 07 LUND, SWEDEN
Email: JoS-...@kuai.se Fax: +46-46-188445


Douglas Bridgens

unread,
Jul 11, 1994, 11:37:01 AM7/11/94
to
Johan Svensson (jo...@ecsdg.lu.se) wrote:

: In article r...@gap.cco.caltech.edu, br...@cco.caltech.edu (Brien M. Oberstein) writes:
: >Here's an easy question, how do you add numbers in a shell script?
: >I want to hav a script which does something like echo '$2+20'
: >ie add 20 to the value of the argument $2
: >
: >
: >-brien

: Try this:

: echo `expr $2 + 20`



This works in the ksh, not sure about others :

echo $(($2 + 20))

unlike the example about this doesn't create a new process.


cheers

Doug

Paul Stephen Borile

unread,
Jul 11, 1994, 12:01:02 PM7/11/94
to
br...@cco.caltech.edu (Brien M. Oberstein) writes:

Try "man expr". You will find out that it is as simple as :

b=`expr $2 + 20`
echo $b

expr(1) also does a lot of other things.

-Paul
--
----------------------------------------------------------------------------
Paul Stephen Borile email : pa...@cusun.sublink.org
: pa...@sixcom.it
: pa...@ghost.dsi.unimi.it

Message has been deleted

David Thomas Richard Given

unread,
Jul 13, 1994, 4:56:25 AM7/13/94
to
In article <2vmn14$r...@gap.cco.caltech.edu>,

Kludge time!

Try:

a=`bc $2 + 20`

--
__ _ _ _ _ | GCS -d+(?)(++) p(-+)(---) c++++ !l+(+)
| \ /_\\ /| | \ / |\ /|_ |\ | | u++ e*(++) m*(++) s !n h+(++) f+ g+
|_/ | | V | |_/ \_| | V |_ | \| | w+(+++) t--(+) r y? (Archimedes owner)
dt...@st-andrews.ac.uk | ***In search of better V's***

Tim Love

unread,
Jul 13, 1994, 6:25:27 AM7/13/94
to
dt...@st-andrews.ac.uk (David Thomas Richard Given) writes:

>In article <2vmn14$r...@gap.cco.caltech.edu>,
>Brien M. Oberstein <br...@cco.caltech.edu> wrote:
>>Here's an easy question, how do you add numbers in a shell script?
>>I want to hav a script which does something like echo '$2+20'
>>ie add 20 to the value of the argument $2

>Kludge time!

>Try:

>a=`bc $2 + 20`

And the korn shell has a builtin called 'let' to do arithmetic.

0 new messages