If no one minds, I'd just like to amplify on this a little. The solution
below uses the primitive Bourne shell method which uses an external
program (expr) to do the arithmetic. Bash has internal functionality to
perform stuff like this. The hashpling at the beginning specifies which
interpreter is to process the script. By specifying your script to use
/bin/sh, you are explicitly *not* writing a bash script.

The script below can be re-written using internal arithmetic functions:

#! /bin/bash
# addnums - adds two numbers input as args and outputs the result
newval=$(($1+$2))
echo "the sum of $1 and $2 is $newval"


-- 
----------Time flies like the wind. Fruit flies like a banana.----------------
--------Stranger things have happened but none stranger than this.-------------
Steven W. Orr      [EMAIL PROTECTED]     <site of former bang addr:-)>
---------------"Listen to me! We are all individuals."-------------------------

On Thu, 27 Jan 2000, Jeff Smelser wrote:

=>this worked great, thanks a lot!
=>
=>                    
=>On Thu, 27 Jan 2000, Bret Hughes wrote:
=>
=>> Try some thing like this
=>> 
=>> 
=>> #! /bin/sh
=>> # addnums - adds two numbers input as args and outputs the result
=>> newval=`expr $1 + $2`
=>> echo 'the sum of ' $1 ' and ' $2 ' is ' $newval 
=>> 
=>> 
=>> Jeff Smelser wrote:
=>> > 
=>> > Just trying to write a bash script and can't seem to figure out how to
=>> > do addition with variables. If someone could give me a quick example, I
=>> > would appreciate it. Thanks


-- 
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.

Reply via email to