[lug] Letting Bash do the Arithmetic

Chip Atkinson chip at rmpg.org
Mon Apr 14 21:07:22 MDT 2003


Here's an example:

#!/bin/bash

let i=5
let j=9

echo $(($i - $j))
echo $((i - j))
echo $((6 - 9))

Note that you don't have to have the $ in front of variables within the
$(( )) construct though it doesn't seem to cause any problems.

Hope that helps.  I really like the "Learning the bash shell" from
O'Reilly myself.

Chip

On Mon, 14 Apr 2003, Peter Hutnick wrote:

> I'm trying to run a command (interactively) and let bash do the grunt
> arithmetic for an argument.  I've found several bash guides that are along
> the lines "oh, just use the ((expression)) construct.  An example sure
> would be nice :-(
>
> So I'm trying stuff like "head -n ((23945837-4926402)) some.file"  and
> getting "bash: syntax error near unexpected token `('."
>
> Not what I am looking for.
>
> Any idea why only people writing dead-tree books seem to think that
> examples are helpful?  I don't think I have ever seen an actual *working*
> example in a man page.  (For reference "foo -x[an integer between -9 and a
> gagillion, default is 42][-r][-s][ [-t [/favorite color/ or /pet's
> name/][--fast]" is not a working example, as the --fast and -r switches
> are mutually exclusive.)
>
> Argh!
>
> -Peter
>
>
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
>




More information about the LUG mailing list