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() {
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
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.
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.
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 ")
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
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'
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
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
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 )
Note that the '-l' option to bc automatically sets the scale to 20.
Check your bc documentation.