Home Search Forums Register Forum Rules Man Pages Unix Commands Linux Commands FAQ Members Search Today's Posts Mark Forums Read

 UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

# how do I get the value of expr with ksh

## UNIX for Advanced & Expert Users

#1
08-24-2001
 krishna Registered User Join Date: Aug 2001 Last Activity: 3 August 2007, 4:25 AM EDT Posts: 60 Thanks: 0 Thanked 0 Times in 0 Posts
how do I get the value of expr with ksh

Hi,

I have written a korn shell script to compute the value of k.

formulae :

a=10
b=20
c=30
k=(a+b)*c

my shell script is :

a=10
b=20
c=30
k=`expr ((\$a + \$b ) * \$c )`
echo \$k

### here paranthesis ( ) not accepting by expr function.
### if i remove paranthesis, the value will be 610(wrong)
### but the actual value should be 900

please give me solution to kkodava@maxis.com.my

thanks

krishna
#2
08-24-2001
 Michael Registered User Join Date: May 2001 Last Activity: 8 November 2001, 8:00 AM EST Location: Germany Posts: 15 Thanks: 0 Thanked 0 Times in 0 Posts
Hi Krisna,

try it with "let"

example:

let k="(\$a + \$b) * \$c"

k=900

greetings
#3
08-25-2001
 patvdv Registered User Join Date: Jul 2001 Last Activity: 14 March 2003, 5:08 AM EST Location: Belgium Posts: 83 Thanks: 0 Thanked 0 Times in 0 Posts
KSH Maths

Why using 'expr' if you can use the built-in KSH math functions instead:

a=10
b=20
c=30
k=\$ (((\$a+\$b)*\$c )))
echo \$k

#4
08-26-2001
 rwb1959 Registered User Join Date: Aug 2001 Last Activity: 16 October 2009, 2:04 PM EDT Location: Virginia, USA Posts: 438 Thanks: 0 Thanked 1 Time in 1 Post
using expr

"expr" evaluates 2 and only 2 expressions.
In order to accomplish what you want to do
using "expr" you need to nest expr's and
of course, remember to "escape" the speecial
characters. An example would be...

echo `expr \`expr 1 + 2\` \* 3`

This expression, when run at the command
prompt, should yield 9. Of course, if you
are using ksh and not planning on running
a different shell, I would stick with the
ksh built-in capabilities.