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

bash alias to add two numbers

129 views
Skip to first unread message

Lao Ming

unread,
Mar 17, 2009, 4:53:42 PM3/17/09
to
This is what I tried:

alias add=$( echo $(( $1 + $2 )) )

but got:

-bash: + : syntax error: operand expected (error token is " ")

I tried substituting tick marks for the "$( )" but got the same.

Can this be done?

pk

unread,
Mar 17, 2009, 5:01:51 PM3/17/09
to

Aliases do not accept parameters, you should use a function:

add() {

Stephane CHAZELAS

unread,
Mar 17, 2009, 5:04:57 PM3/17/09
to
2009-03-17, 13:53(-07), Lao Ming:
[...]

add() (IFS=+; echo $(($*)))

--
Stéphane

Lao Ming

unread,
Mar 17, 2009, 5:57:33 PM3/17/09
to
On Mar 17, 2:04 pm, Stephane CHAZELAS <stephane_chaze...@yahoo.fr>
wrote:
> St phane

Great!

Okay, I know you didn't say it would work with
other arithmetic but I figured that
once I knew one way, the others would
be simple slight modifications.


Thus, subtraction:

sub() (IFS='-'; echo $(($*)))

makes negative results fail.

sub 5 2
3

sub 2 5
3

Multiplication works but not with real numbers:

mult() (IFS='*'; echo $(($*)))

mult 10 -3
-30

mult 10.3 8.8
-bash: 10.3*8.8: syntax error in expression (error token is ".3*8.8")

I couldn't get division to work either. I also tried escaping the
minus sign, the asterisk and the slash but still got errors.

Lao Ming

unread,
Mar 17, 2009, 6:15:10 PM3/17/09
to

Thanks but this also fails with real numbers:

add 4 4.2
-bash: 4 + 4.2 : syntax error in expression (error token is ".2 ")

bash --version
GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0)
Copyright (C) 2002 Free Software Foundation, Inc.

Lao Ming

unread,
Mar 17, 2009, 6:19:25 PM3/17/09
to

It also fails on a newer version of bash:

bash --version
GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)
Copyright (C) 2005 Free Software Foundation, Inc.

add() {
> echo $(( $1 + $2 ))
> }

add 2 2.2
-bash: 2 + 2.2 : syntax error: invalid arithmetic operator (error
token is ".2 ")

add 2 2\.2
-bash: 2 + 2.2 : syntax error: invalid arithmetic operator (error
token is ".2 ")


Janis Papanagnou

unread,
Mar 17, 2009, 6:21:57 PM3/17/09
to
Lao Ming wrote:
> On Mar 17, 2:01 pm, pk <p...@pk.invalid> wrote:
>
>>On Tuesday 17 March 2009 21:53, Lao Ming wrote:
>>
>>
>>>This is what I tried:
>>
>>> alias add=$( echo $(( $1 + $2 )) )
>>
>>>but got:
>>
>>> -bash: + : syntax error: operand expected (error token is " ")
>>
>>>I tried substituting tick marks for the "$( )" but got the same.
>>
>>>Can this be done?
>>
>>Aliases do not accept parameters, you should use a function:
>>
>>add() {
>> echo $(( $1 + $2 ))
>>
>>}
>
>
> Thanks but this also fails with real numbers:

You haven't read the bash man page?

ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated,
under certain circumstances (see the let builtin command
and Arithmetic Expansion). Evaluation is done in fixed-
width integers with no check for overflow, though division
by 0 is trapped and flagged as an error. The operators
and their precedence and associativity are the same as in
the C language. The following list of operators is
grouped into levels of equal-precedence operators. The
levels are listed in order of decreasing precedence.


Use another shell to do float arithmetics, e.g., ksh93.
Or use an external program for the calculation, e.g. bc.

Janis

Michael Tosch

unread,
Mar 17, 2009, 6:46:19 PM3/17/09
to
Lao Ming wrote:
> On Mar 17, 2:04 pm, Stephane CHAZELAS <stephane_chaze...@yahoo.fr>
> wrote:
>> 2009-03-17, 13:53(-07), Lao Ming:> This is what I tried:
>>
>>> alias add=$( echo $(( $1 + $2 )) )
>>> but got:
>>> -bash: + : syntax error: operand expected (error token is " ")
>>> I tried substituting tick marks for the "$( )" but got the same.
>> [...]
>>
>> add() (IFS=+; echo $(($*)))
>>
>> --
>> St phane
>
> Great!
>
> Okay, I know you didn't say it would work with
> other arithmetic but I figured that
> once I knew one way, the others would
> be simple slight modifications.
>
>
> Thus, subtraction:
>
> sub() (IFS='-'; echo $(($*)))
>
> makes negative results fail.
>
> sub 5 2
> 3
>
> sub 2 5
> 3

The result is -3 but during printing the IFS='-' turns
the - into a space char.

Work-around:
sub() (IFS='-'; res=$*; IFS=' '; echo $(($res)))

The purpose of the IFS complication is to support more args
e.g.

sub 2 4 1
add 3 4 5 6

bash's builtin $(( )) works only with integers.
For decimals, you need the external tool dc (or bc)
e.g.

add() (echo $* + p | dc)
add 2 4.5 8


--
echo imhcea\.lophc.tcs.hmo |
sed 's2\(....\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1s;\.;::;2'

Michael Tosch

unread,
Mar 17, 2009, 7:39:47 PM3/17/09
to
Oops, this only takes the last two args.
Who knows dc better?

With printf it works:

add() {
(echo 0; printf "%s+\n" $*; echo p) | dc

Glenn Jackman

unread,
Mar 18, 2009, 10:20:27 AM3/18/09
to
At 2009-03-17 07:39PM, "Michael Tosch" wrote:

> Michael Tosch wrote:
> > For decimals, you need the external tool dc (or bc)
> > e.g.
> >
> > add() (echo $* + p | dc)
> > add 2 4.5 8
> >
> Oops, this only takes the last two args.
> Who knows dc better?
>
> With printf it works:
>
> add() {
> (echo 0; printf "%s+\n" $*; echo p) | dc
> }

Might be easier with bc:

$ add() ( IFS='+'; echo "$*" | bc -l )
$ add 2 4.5 8
14.5

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

Stephane CHAZELAS

unread,
Mar 18, 2009, 1:56:39 PM3/18/09
to
2009-03-17, 14:57(-07), Lao Ming:
[...]

> sub() (IFS='-'; echo $(($*)))
>
> makes negative results fail.
>
> sub 5 2
> 3
>
> sub 2 5
> 3

sub() (IFS='-'; echo "$(($*))")

>
> Multiplication works but not with real numbers:
>
> mult() (IFS='*'; echo $(($*)))
>
> mult 10 -3
> -30
>
> mult 10.3 8.8
> -bash: 10.3*8.8: syntax error in expression (error token is ".3*8.8")

[...]

bash arithmetics only supports integers

If you want to support floats:

mult() (IFS=*; echo "$*" | bc -l)

$ mult 2 2 2 2 2.0002
32.0032

--
Stéphane

Lao Ming

unread,
Mar 19, 2009, 8:59:22 PM3/19/09
to
On Mar 18, 10:56 am, Stephane CHAZELAS <stephane_chaze...@yahoo.fr>
wrote:

This is perfect and it works in all four. Thanks.

add() ( IFS='+'; echo "$*" |bc -l )

sub() ( IFS='-'; echo "$*" |bc -l )
mult() ( IFS='*'; echo "$*" |bc -l )
div() ( IFS='/'; echo "scale=4; $*" |bc -l )

Glenn Jackman

unread,
Mar 20, 2009, 11:41:47 AM3/20/09
to
At 2009-03-19 08:59PM, "Lao Ming" wrote:
> div() ( IFS='/'; echo "scale=4; $*" |bc -l )

Note that the '-l' option to bc automatically sets the scale to 20.
Check your bc documentation.

0 new messages